Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Decoupling] Unblinding of SignedBlockContainer (part 3) #7165

Merged

This file was deleted.

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.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;

public interface BlockFactory {
Expand All @@ -31,6 +31,6 @@ SafeFuture<BlockContainer> createUnsignedBlock(
Optional<Bytes32> optionalGraffiti,
boolean blinded);

SafeFuture<SignedBeaconBlock> unblindSignedBeaconBlockIfBlinded(
SignedBeaconBlock blindedSignedBeaconBlock);
SafeFuture<SignedBlockContainer> unblindSignedBlockIfBlinded(
SignedBlockContainer maybeBlindedBlockContainer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package tech.pegasys.teku.validator.coordinator;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes32;
Expand All @@ -23,10 +24,16 @@
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlindedBlobSidecar;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecar;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.SignedBlindedBlobSidecar;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.SignedBlobSidecar;
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.SignedBlindedBlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.BlindedBlockContents;
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.schemas.SchemaDefinitionsDeneb;

Expand Down Expand Up @@ -68,6 +75,18 @@ public SafeFuture<BlockContainer> createUnsignedBlock(
});
}

@Override
public SafeFuture<SignedBlockContainer> unblindSignedBlockIfBlinded(
final SignedBlockContainer maybeBlindedBlockContainer) {
if (maybeBlindedBlockContainer.isBlinded()) {
return unblindBlock(maybeBlindedBlockContainer)
.thenCombine(
unblindBlobSidecars(maybeBlindedBlockContainer),
this::createUnblindedSignedBlockContents);
}
return SafeFuture.completedFuture(maybeBlindedBlockContainer);
}

private BlockContents createBlockContents(
final BeaconBlock block, final List<BlobSidecar> blobSidecars) {
return schemaDefinitionsDeneb.getBlockContentsSchema().create(block, blobSidecars);
Expand All @@ -79,4 +98,30 @@ private BlindedBlockContents createBlindedBlockContents(
.getBlindedBlockContentsSchema()
.create(block, blindedBlobSidecars);
}

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

private SafeFuture<List<SignedBlobSidecar>> unblindBlobSidecars(
final SignedBlockContainer blindedBlockContainer) {
final UInt64 slot = blindedBlockContainer.getSlot();
final List<SignedBlindedBlobSidecar> blindedBlobSidecars =
blindedBlockContainer
.toBlinded()
.flatMap(SignedBlindedBlockContainer::getSignedBlindedBlobSidecars)
.orElse(Collections.emptyList());
return spec.unblindSignedBlindedBlobSidecars(
slot, blindedBlobSidecars, operationSelector.createBlobSidecarsUnblinderSelector(slot));
}

private SignedBlockContents createUnblindedSignedBlockContents(
final SignedBeaconBlock signedBlock, final List<SignedBlobSidecar> signedBlobSidecars) {
return schemaDefinitionsDeneb
.getSignedBlockContentsSchema()
.create(signedBlock, signedBlobSidecars);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@
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.state.beaconstate.BeaconState;

public class BlockFactoryPhase0 extends AbstractBlockFactory {
public class BlockFactoryPhase0 implements BlockFactory {

protected final Spec spec;
protected final BlockOperationSelectorFactory operationSelector;

public BlockFactoryPhase0(
final Spec spec, final BlockOperationSelectorFactory operationSelector) {
super(spec, operationSelector);
this.spec = spec;
this.operationSelector = operationSelector;
}

@Override
Expand Down Expand Up @@ -60,4 +66,16 @@ public SafeFuture<BlockContainer> createUnsignedBlock(
blinded)
.thenApply(BeaconBlockAndState::getBlock);
}

@Override
public SafeFuture<SignedBlockContainer> unblindSignedBlockIfBlinded(
final SignedBlockContainer maybeBlindedBlockContainer) {
if (maybeBlindedBlockContainer.isBlinded()) {
return spec.unblindSignedBeaconBlock(
maybeBlindedBlockContainer.getSignedBlock(),
operationSelector.createBlockUnblinderSelector())
.thenApply(Function.identity());
}
return SafeFuture.completedFuture(maybeBlindedBlockContainer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.datastructures.blobs.SignedBlobSidecarsUnblinder;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlindedBlobSidecar;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlindedBlobSidecarSchema;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecar;
Expand Down Expand Up @@ -304,7 +305,7 @@ private void builderSetKzgCommitments(
bodyBuilder.blobKzgCommitments(blobKzgCommitments);
}

public Consumer<SignedBeaconBlockUnblinder> createUnblinderSelector() {
public Consumer<SignedBeaconBlockUnblinder> createBlockUnblinderSelector() {
return bodyUnblinder -> {
final BeaconBlock block = bodyUnblinder.getSignedBlindedBeaconBlock().getMessage();

Expand All @@ -330,6 +331,12 @@ public Consumer<SignedBeaconBlockUnblinder> createUnblinderSelector() {
};
}

public Consumer<SignedBlobSidecarsUnblinder> createBlobSidecarsUnblinderSelector(
final UInt64 slot) {
return blobSidecarsUnblinder ->
blobSidecarsUnblinder.setBlobsBundleSupplier(() -> getCachedBlobsBundle(slot));
}

public Function<BeaconBlock, SafeFuture<List<BlobSidecar>>> createBlobSidecarsSelector() {
return block -> {
final BlobSidecarSchema blobSidecarSchema =
Expand All @@ -347,9 +354,9 @@ public Function<BeaconBlock, SafeFuture<List<BlobSidecar>>> createBlobSidecarsSe
block.getSlot(),
block.getParentRoot(),
block.getProposerIndex(),
blobsBundle.getBlobs().get(index).getBytes(),
blobsBundle.getCommitments().get(index).getBytesCompressed(),
blobsBundle.getProofs().get(index).getBytesCompressed()))
blobsBundle.getBlobs().get(index),
blobsBundle.getCommitments().get(index),
blobsBundle.getProofs().get(index)))
.collect(Collectors.toUnmodifiableList()));
};
}
Expand All @@ -373,8 +380,8 @@ public Function<BeaconBlock, SafeFuture<List<BlobSidecar>>> createBlobSidecarsSe
block.getParentRoot(),
block.getProposerIndex(),
blobsBundle.getBlobs().get(index).hashTreeRoot(),
blobsBundle.getCommitments().get(index).getBytesCompressed(),
blobsBundle.getProofs().get(index).getBytesCompressed()))
blobsBundle.getCommitments().get(index),
blobsBundle.getProofs().get(index)))
.collect(Collectors.toUnmodifiableList()));
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@
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.state.beaconstate.BeaconState;

public class MilestoneBasedBlockFactory extends AbstractBlockFactory {
public class MilestoneBasedBlockFactory implements BlockFactory {

private final Map<SpecMilestone, BlockFactory> registeredFactories = new HashMap<>();

private final Spec spec;

public MilestoneBasedBlockFactory(
final Spec spec, final BlockOperationSelectorFactory operationSelector) {
super(spec, operationSelector);
this.spec = spec;
final BlockFactoryPhase0 blockFactoryPhase0 = new BlockFactoryPhase0(spec, operationSelector);

// Not needed for all milestones
Expand All @@ -60,9 +63,22 @@ public SafeFuture<BlockContainer> createUnsignedBlock(
final BLSSignature randaoReveal,
final Optional<Bytes32> optionalGraffiti,
final boolean blinded) {
final SpecMilestone milestone = spec.atSlot(newSlot).getMilestone();
final SpecMilestone milestone = getMilestone(newSlot);
return registeredFactories
.get(milestone)
.createUnsignedBlock(blockSlotState, newSlot, randaoReveal, optionalGraffiti, blinded);
}

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

private SpecMilestone getMilestone(final UInt64 slot) {
return spec.atSlot(slot).getMilestone();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
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 @@ -98,12 +99,12 @@ public DefaultPerformanceTracker(
}

@Override
public void start(UInt64 nodeStartSlot) {
public void start(final UInt64 nodeStartSlot) {
this.nodeStartEpoch = Optional.of(spec.computeEpochAtSlot(nodeStartSlot));
}

@Override
public void onSlot(UInt64 slot) {
public void onSlot(final UInt64 slot) {
// Ensure a consistent view as the field is volatile.
final Optional<UInt64> nodeStartEpoch = this.nodeStartEpoch;
if (nodeStartEpoch.isEmpty() || combinedChainDataClient.getChainHead().isEmpty()) {
Expand Down Expand Up @@ -199,7 +200,7 @@ private SafeFuture<?> reportAttestationPerformance(final UInt64 currentEpoch) {
});
}

private SafeFuture<BlockPerformance> getBlockPerformanceForEpoch(UInt64 currentEpoch) {
private SafeFuture<BlockPerformance> getBlockPerformanceForEpoch(final UInt64 currentEpoch) {
return combinedChainDataClient
.getChainHead()
.orElseThrow()
Expand Down Expand Up @@ -345,7 +346,7 @@ private AttestationPerformance calculateAttestationPerformance(
}

private SafeFuture<Set<BeaconBlock>> getBlocksInEpochs(
UInt64 startEpochInclusive, UInt64 endEpochExclusive) {
final UInt64 startEpochInclusive, final UInt64 endEpochExclusive) {
final UInt64 epochStartSlot = spec.computeStartSlotAtEpoch(startEpochInclusive);
final UInt64 inclusiveEndEpochEndSlot =
spec.computeStartSlotAtEpoch(endEpochExclusive).decrement();
Expand Down Expand Up @@ -382,7 +383,7 @@ private SafeFuture<Boolean> fillBlockInEffectAtSlot(
}

private SafeFuture<Map<UInt64, List<Attestation>>> getAttestationsIncludedInEpochs(
UInt64 startEpochInclusive, UInt64 endEpochExclusive) {
final UInt64 startEpochInclusive, final UInt64 endEpochExclusive) {
return getBlocksInEpochs(startEpochInclusive, endEpochExclusive)
.thenApply(
beaconBlocks ->
Expand All @@ -394,24 +395,24 @@ private SafeFuture<Map<UInt64, List<Attestation>>> getAttestationsIncludedInEpoc
}

@Override
public void saveProducedAttestation(Attestation attestation) {
UInt64 epoch = spec.computeEpochAtSlot(attestation.getData().getSlot());
Set<Attestation> attestationsInEpoch =
public void saveProducedAttestation(final Attestation attestation) {
final UInt64 epoch = spec.computeEpochAtSlot(attestation.getData().getSlot());
final Set<Attestation> attestationsInEpoch =
producedAttestationsByEpoch.computeIfAbsent(epoch, __ -> concurrentSet());
attestationsInEpoch.add(attestation);
}

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

@Override
public void reportBlockProductionAttempt(UInt64 epoch) {
AtomicInteger numberOfBlockProductionAttempts =
final AtomicInteger numberOfBlockProductionAttempts =
blockProductionAttemptsByEpoch.computeIfAbsent(epoch, __ -> new AtomicInteger(0));
numberOfBlockProductionAttempts.incrementAndGet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@

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

public class NoOpPerformanceTracker implements PerformanceTracker {

@Override
public void start(UInt64 nodeStartSlot) {}
public void start(final UInt64 nodeStartSlot) {}

@Override
public void saveProducedAttestation(Attestation attestation) {}
public void saveProducedAttestation(final Attestation attestation) {}

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

@Override
public void reportBlockProductionAttempt(UInt64 epoch) {}
public void reportBlockProductionAttempt(final UInt64 epoch) {}

@Override
public void saveExpectedSyncCommitteeParticipant(
Expand All @@ -41,5 +41,5 @@ public void saveExpectedSyncCommitteeParticipant(
public void saveProducedSyncCommitteeMessage(final SyncCommitteeMessage message) {}

@Override
public void onSlot(UInt64 slot) {}
public void onSlot(final UInt64 slot) {}
}
Loading