From b61574c528e18879302b0c6beb1d7e00f6098d10 Mon Sep 17 00:00:00 2001 From: Paul Harris Date: Fri, 2 Feb 2024 06:21:18 +1000 Subject: [PATCH] tiny refactors for late block reorg (#7939) adjunct to #6595 Signed-off-by: Paul Harris --- .../teku/validator/coordinator/ValidatorApiHandler.java | 3 ++- .../spec/src/main/java/tech/pegasys/teku/spec/Spec.java | 6 +++--- .../teku/spec/logic/common/util/BlockProposalUtil.java | 8 ++++---- .../teku/services/beaconchain/BeaconChainController.java | 6 +++++- .../beaconchain/ValidatorIsConnectedProviderImpl.java | 7 ++++--- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/beacon/validator/src/main/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandler.java b/beacon/validator/src/main/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandler.java index 1ca282eb6d1..3ad28edf7b2 100644 --- a/beacon/validator/src/main/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandler.java +++ b/beacon/validator/src/main/java/tech/pegasys/teku/validator/coordinator/ValidatorApiHandler.java @@ -361,7 +361,8 @@ private SafeFuture> createBlock( return SafeFuture.completedFuture(Optional.empty()); } final BeaconState blockSlotState = maybeBlockSlotState.get(); - final Bytes32 parentRoot = spec.getBlockRootAtSlot(blockSlotState, slot.minus(1)); + final Bytes32 parentRoot = spec.getBlockRootAtSlot(blockSlotState, slot.decrement()); + LOG.debug("parent block {}:({})", parentRoot, slot); if (combinedChainDataClient.isOptimisticBlock(parentRoot)) { LOG.warn( "Unable to produce block at slot {} because parent has optimistically validated payload", diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java index e4211378c11..896a1e42729 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java @@ -696,16 +696,16 @@ public BeaconState processSlots(BeaconState preState, UInt64 slot) // Block Proposal public SafeFuture createNewUnsignedBlock( - final UInt64 newSlot, + final UInt64 proposalSlot, final int proposerIndex, final BeaconState blockSlotState, final Bytes32 parentBlockSigningRoot, final Function> bodyBuilder, final BlockProductionPerformance blockProductionPerformance) { - return atSlot(newSlot) + return atSlot(proposalSlot) .getBlockProposalUtil() .createNewUnsignedBlock( - newSlot, + proposalSlot, proposerIndex, blockSlotState, parentBlockSigningRoot, diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/util/BlockProposalUtil.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/util/BlockProposalUtil.java index de95b4c61ee..0b976fbe370 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/util/BlockProposalUtil.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/util/BlockProposalUtil.java @@ -47,16 +47,16 @@ public BlockProposalUtil( } public SafeFuture createNewUnsignedBlock( - final UInt64 newSlot, + final UInt64 proposalSlot, final int proposerIndex, final BeaconState blockSlotState, final Bytes32 parentBlockSigningRoot, final Function> bodyBuilder, final BlockProductionPerformance blockProductionPerformance) { checkArgument( - blockSlotState.getSlot().equals(newSlot), + blockSlotState.getSlot().equals(proposalSlot), "Block slot state from incorrect slot. Expected %s but got %s", - newSlot, + proposalSlot, blockSlotState.getSlot()); // Create block body @@ -72,7 +72,7 @@ public SafeFuture createNewUnsignedBlock( ? schemaDefinitions.getBlindedBeaconBlockSchema() : schemaDefinitions.getBeaconBlockSchema(); return beaconBlockSchema.create( - newSlot, + proposalSlot, UInt64.valueOf(proposerIndex), parentBlockSigningRoot, tmpStateRoot, diff --git a/services/beaconchain/src/main/java/tech/pegasys/teku/services/beaconchain/BeaconChainController.java b/services/beaconchain/src/main/java/tech/pegasys/teku/services/beaconchain/BeaconChainController.java index 6e0836e3356..ef29ad76758 100644 --- a/services/beaconchain/src/main/java/tech/pegasys/teku/services/beaconchain/BeaconChainController.java +++ b/services/beaconchain/src/main/java/tech/pegasys/teku/services/beaconchain/BeaconChainController.java @@ -175,6 +175,7 @@ import tech.pegasys.teku.storage.client.EarliestAvailableBlockSlot; import tech.pegasys.teku.storage.client.RecentChainData; import tech.pegasys.teku.storage.client.StorageBackedRecentChainData; +import tech.pegasys.teku.storage.client.ValidatorIsConnectedProvider; import tech.pegasys.teku.storage.store.FileKeyValueStore; import tech.pegasys.teku.storage.store.KeyValueStore; import tech.pegasys.teku.storage.store.StoreConfig; @@ -397,6 +398,9 @@ protected SafeFuture initialize() { storageQueryChannel = combinedStorageChannel; storageUpdateChannel = combinedStorageChannel; final VoteUpdateChannel voteUpdateChannel = eventChannels.getPublisher(VoteUpdateChannel.class); + + final ValidatorIsConnectedProvider validatorIsConnectedProvider = + new ValidatorIsConnectedProviderImpl(() -> forkChoiceNotifier); // Init other services return initWeakSubjectivity(storageQueryChannel, storageUpdateChannel) .thenCompose( @@ -414,7 +418,7 @@ protected SafeFuture initialize() { voteUpdateChannel, eventChannels.getPublisher(FinalizedCheckpointChannel.class, beaconAsyncRunner), coalescingChainHeadChannel, - new ValidatorIsConnectedProviderImpl(forkChoiceNotifier), + validatorIsConnectedProvider, spec)) .thenCompose( client -> { diff --git a/services/beaconchain/src/main/java/tech/pegasys/teku/services/beaconchain/ValidatorIsConnectedProviderImpl.java b/services/beaconchain/src/main/java/tech/pegasys/teku/services/beaconchain/ValidatorIsConnectedProviderImpl.java index b21612a11d2..3dfe174e6f8 100644 --- a/services/beaconchain/src/main/java/tech/pegasys/teku/services/beaconchain/ValidatorIsConnectedProviderImpl.java +++ b/services/beaconchain/src/main/java/tech/pegasys/teku/services/beaconchain/ValidatorIsConnectedProviderImpl.java @@ -13,19 +13,20 @@ package tech.pegasys.teku.services.beaconchain; +import java.util.function.Supplier; import tech.pegasys.teku.infrastructure.unsigned.UInt64; import tech.pegasys.teku.statetransition.forkchoice.ForkChoiceNotifier; import tech.pegasys.teku.storage.client.ValidatorIsConnectedProvider; public class ValidatorIsConnectedProviderImpl implements ValidatorIsConnectedProvider { - private final ForkChoiceNotifier forkChoiceNotifier; + private final Supplier forkChoiceNotifier; - public ValidatorIsConnectedProviderImpl(ForkChoiceNotifier forkChoiceNotifier) { + public ValidatorIsConnectedProviderImpl(Supplier forkChoiceNotifier) { this.forkChoiceNotifier = forkChoiceNotifier; } @Override public boolean isValidatorConnected(int validatorId, UInt64 slot) { - return forkChoiceNotifier.validatorIsConnected(UInt64.valueOf(validatorId), slot); + return forkChoiceNotifier.get().validatorIsConnected(UInt64.valueOf(validatorId), slot); } }