Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,23 @@ public interface ReadOnlyForkChoiceStrategy {
Optional<Bytes32> getAncestor(Bytes32 blockRoot, UInt64 slot);

/**
* Walks the parent chain from blockRoot to find the ancestor node at the given slot.
* Walks the parent chain from {@code node} to find the ancestor node at the given slot.
*
* <p>This mirrors the Gloas {@code get_ancestor(...)} helper, which returns a full {@link
* ForkChoiceNode} identity.
* <p>This mirrors the {@code get_ancestor(store, node, slot)} helper, which takes and returns a
* {@link ForkChoiceNode} identity. The starting node's payload status selects which parent
* variant to follow, and the node itself is returned once its block slot is at or before {@code
* slot}.
*
* <p>This is the payload-status-aware counterpart of {@link #getAncestor(Bytes32, UInt64)} and is
* intentionally abstract: unlike a block-root walk it cannot be derived from {@link
* #getAncestor(Bytes32, UInt64)}, because that would discard each ancestor's payload status and
* always yield {@linkplain ForkChoiceNode#createBase(Bytes32) base (PENDING)} nodes — which
* silently breaks the Gloas ancestry checks in {@code ForkChoiceUtilGloas.isAncestor}. Every
* implementation must therefore resolve payload status explicitly. Implementations that do not
* model the Gloas three-state tree may resolve ancestry by block root and return base (PENDING)
* nodes.
*/
default Optional<ForkChoiceNode> getAncestorNode(final Bytes32 blockRoot, final UInt64 slot) {
return getAncestor(blockRoot, slot).map(ForkChoiceNode::createBase);
}
Optional<ForkChoiceNode> getAncestorNode(ForkChoiceNode node, UInt64 slot);

Optional<ForkChoiceNode> getParentBeaconBlockNode(ForkChoiceNode node);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,35 @@ public Optional<Bytes32> getAncestor(
return forkChoiceStrategy.getAncestor(root, slot);
}

public Optional<ForkChoiceNode> getAncestorNode(
final ReadOnlyForkChoiceStrategy forkChoiceStrategy, final Bytes32 root, final UInt64 slot) {
return getAncestor(forkChoiceStrategy, root, slot).map(ForkChoiceNode::createBase);
/**
* is_ancestor
*
* <p>Returns whether {@code ancestor} is an ancestor of {@code node}, i.e. {@code ancestor} lies
* on the chain from {@code node} at {@code ancestor}'s block slot.
*
* <p>This base (phase0) form is the {@code is_ancestor#phase0} spec anchor and the default that
* {@code ForkChoiceUtilGloas#isAncestor} overrides. It has no pre-Gloas caller by design: in the
* spec, {@code is_ancestor} is consumed only by {@code get_weight}, to add proposer boost when a
* node is an ancestor of the proposer-boost node. Teku instead computes {@code get_weight} inside
* protoarray, folding the proposer-boost delta into each node's stored weight during {@code
* ProtoArray#applyScoreChanges}. That back-propagation realizes the ancestry condition
* implicitly, so the weight path needs no explicit {@code is_ancestor} call. The only explicit
* caller is the Gloas override, which runs the check in reverse to recover the attestation-only
* weight (via {@code ForkChoiceUtilGloas#getNodeAttestationWeight}); pre-Gloas {@code
* isHeadWeak}/{@code isParentStrong} read the already-boosted protoarray weight directly and
* never query ancestry. Since {@code ForkChoiceUtilGloas} overrides this method, even that
* reverse path dispatches to the override rather than here — so this base form is retained for
* spec fidelity and exercised only by its unit test.
*/
public boolean isAncestor(
final ReadOnlyForkChoiceStrategy forkChoiceStrategy,
final ForkChoiceNode node,
final ForkChoiceNode ancestor) {
return forkChoiceStrategy
.blockSlot(ancestor.blockRoot())
.flatMap(ancestorSlot -> forkChoiceStrategy.getAncestorNode(node, ancestorSlot))
.map(ancestor::equals)
.orElse(false);
}

public NavigableMap<UInt64, Bytes32> getAncestors(
Expand Down Expand Up @@ -155,14 +181,11 @@ public NavigableMap<UInt64, Bytes32> getAncestorsOnFork(
}

/**
* is_shuffling_stable
*
* <p>Refer to fork-choice specification.
* is_not_epoch_boundary
*
* @param slot
* @return
* @return {@code true} when {@code slot} is NOT at an epoch boundary.
*/
public boolean isShufflingStable(final UInt64 slot) {
public boolean isNotEpochBoundary(final UInt64 slot) {
return !slot.mod(specConfig.getSlotsPerEpoch()).isZero();
}

Expand Down Expand Up @@ -359,7 +382,7 @@ boolean isSingleSlotReorg(
}

boolean isForkChoiceStableAndFinalizationOk(final ReadOnlyStore store, final UInt64 slot) {
return isShufflingStable(slot) && isFinalizationOk(store, slot);
return isNotEpochBoundary(slot) && isFinalizationOk(store, slot);
}

boolean isProposerBoostActive(final ReadOnlyStore store, final Bytes32 headRoot) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,31 @@ public boolean shouldApplyProposerBoost(
return true;
}

/**
* is_ancestor
*
* <p>[Modified in Gloas:EIP7732] In addition to matching the ancestor block root, the resolved
* ancestor's payload status must equal {@code ancestor}'s, unless {@code ancestor} is PENDING
* (which matches any payload status).
*/
@Override
public Optional<ForkChoiceNode> getAncestorNode(
final ReadOnlyForkChoiceStrategy forkChoiceStrategy, final Bytes32 root, final UInt64 slot) {
return forkChoiceStrategy.getAncestorNode(root, slot);
public boolean isAncestor(
final ReadOnlyForkChoiceStrategy forkChoiceStrategy,
final ForkChoiceNode node,
final ForkChoiceNode ancestor) {
final Optional<UInt64> ancestorSlot = forkChoiceStrategy.blockSlot(ancestor.blockRoot());
if (ancestorSlot.isEmpty()) {
return false;
}
final Optional<ForkChoiceNode> maybeNodeAncestor =
forkChoiceStrategy.getAncestorNode(node, ancestorSlot.get());
if (maybeNodeAncestor.isEmpty()) {
return false;
}
final ForkChoiceNode nodeAncestor = maybeNodeAncestor.get();
return nodeAncestor.blockRoot().equals(ancestor.blockRoot())
&& (nodeAncestor.payloadStatus() == ancestor.payloadStatus()
|| ancestor.payloadStatus() == PAYLOAD_STATUS_PENDING);
}

/**
Expand Down Expand Up @@ -359,12 +380,12 @@ private boolean protoArrayWeightIncludesProposerBoost(
final Bytes32 nodeRoot,
final ForkChoicePayloadStatus nodePayloadStatus,
final Bytes32 proposerBoostRoot) {
return forkChoiceStrategy
.blockSlot(nodeRoot)
.flatMap(nodeSlot -> getAncestorNode(forkChoiceStrategy, proposerBoostRoot, nodeSlot))
.filter(
ancestorNode -> ancestorNode.equals(new ForkChoiceNode(nodeRoot, nodePayloadStatus)))
.isPresent();
// Spec mapping: get_weight applies proposer boost to ``node`` when ``node`` is an ancestor of
// the PENDING proposer-boost node.
return isAncestor(
forkChoiceStrategy,
ForkChoiceNode.createBase(proposerBoostRoot),
new ForkChoiceNode(nodeRoot, nodePayloadStatus));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,47 @@ void isDataAvailabilityCheckDeferredToExecutionPayloadEnvelope_shouldDefaultToFa
.isFalse();
}

@Test
void isAncestor_returnsTrueWhenResolvedAncestorMatches() {
final ReadOnlyForkChoiceStrategy strategy = mock(ReadOnlyForkChoiceStrategy.class);
final Bytes32 ancestorRoot = dataStructureUtil.randomBytes32();
final ForkChoiceNode node = ForkChoiceNode.createBase(dataStructureUtil.randomBytes32());
final ForkChoiceNode ancestor = ForkChoiceNode.createBase(ancestorRoot);
final UInt64 ancestorSlot = UInt64.valueOf(3);

when(strategy.blockSlot(ancestorRoot)).thenReturn(Optional.of(ancestorSlot));
when(strategy.getAncestorNode(node, ancestorSlot)).thenReturn(Optional.of(ancestor));

assertThat(forkChoiceUtil.isAncestor(strategy, node, ancestor)).isTrue();
}

@Test
void isAncestor_returnsFalseWhenResolvedAncestorDiffers() {
final ReadOnlyForkChoiceStrategy strategy = mock(ReadOnlyForkChoiceStrategy.class);
final Bytes32 ancestorRoot = dataStructureUtil.randomBytes32();
final ForkChoiceNode node = ForkChoiceNode.createBase(dataStructureUtil.randomBytes32());
final ForkChoiceNode ancestor = ForkChoiceNode.createBase(ancestorRoot);
final UInt64 ancestorSlot = UInt64.valueOf(3);

when(strategy.blockSlot(ancestorRoot)).thenReturn(Optional.of(ancestorSlot));
when(strategy.getAncestorNode(node, ancestorSlot))
.thenReturn(Optional.of(ForkChoiceNode.createBase(dataStructureUtil.randomBytes32())));

assertThat(forkChoiceUtil.isAncestor(strategy, node, ancestor)).isFalse();
}

@Test
void isAncestor_returnsFalseWhenAncestorSlotUnknown() {
final ReadOnlyForkChoiceStrategy strategy = mock(ReadOnlyForkChoiceStrategy.class);
final Bytes32 ancestorRoot = dataStructureUtil.randomBytes32();
final ForkChoiceNode node = ForkChoiceNode.createBase(dataStructureUtil.randomBytes32());
final ForkChoiceNode ancestor = ForkChoiceNode.createBase(ancestorRoot);

when(strategy.blockSlot(ancestorRoot)).thenReturn(Optional.empty());

assertThat(forkChoiceUtil.isAncestor(strategy, node, ancestor)).isFalse();
}

@Test
void getAncestors_shouldGetSimpleSequenceOfAncestors() {
chainBuilder.generateBlocksUpToSlot(10);
Expand Down Expand Up @@ -268,24 +309,24 @@ void canOptimisticallyImport_shouldBeFalseWhenBlockIsMergeButNotOldEnough() {
}

@ParameterizedTest
@MethodSource("isShufflingStableConditions")
void isShufflingStable(final int slot, final boolean expectedResult) {
assertThat(forkChoiceUtil.isShufflingStable(UInt64.valueOf(slot))).isEqualTo(expectedResult);
@MethodSource("isNotEpochBoundaryConditions")
void isNotEpochBoundary(final int slot, final boolean expectedResult) {
assertThat(forkChoiceUtil.isNotEpochBoundary(UInt64.valueOf(slot))).isEqualTo(expectedResult);
}

public static Stream<Arguments> isShufflingStableConditions() {
public static Stream<Arguments> isNotEpochBoundaryConditions() {
// 8 slots per epoch for test conditions
final int epochStart = 10240 * 8;
final int nextEpochStart = 10241 * 8;
// slot , expectedResult
final ArrayList<Arguments> args = new ArrayList<>();

// shuffling is not stable at any epoch boundary
// is_not_epoch_boundary returns false at any epoch boundary
args.add(Arguments.of(0, false));
args.add(Arguments.of(epochStart, false));
args.add(Arguments.of(nextEpochStart, false));
for (int i = epochStart + 1; i < nextEpochStart; i++) {
// all non epoch boundary slots are considered stable
// all non epoch boundary slots return true
args.add(Arguments.of(i, true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ void isHeadWeak_shouldIgnoreAppliedProposerBoostWhenUsingProtoArrayWeight() {
when(strategy.getBlockData(headRoot, ForkChoicePayloadStatus.PAYLOAD_STATUS_PENDING))
.thenReturn(Optional.of(headNode));
when(strategy.blockSlot(headRoot)).thenReturn(Optional.of(gloasSlot));
when(strategy.getAncestorNode(boostRoot, gloasSlot))
when(strategy.getAncestorNode(ForkChoiceNode.createBase(boostRoot), gloasSlot))
.thenReturn(Optional.of(ForkChoiceNode.createBase(headRoot)));

assertThat(forkChoiceUtil.isHeadWeak(store, headRoot, UInt64.ONE)).isTrue();
Expand Down Expand Up @@ -475,7 +475,7 @@ void isParentStrong_shouldIgnoreAppliedProposerBoostWhenEvaluatingResolvedParent
when(strategy.getBlockData(parentRoot, ForkChoicePayloadStatus.PAYLOAD_STATUS_FULL))
.thenReturn(Optional.of(parentNode));
when(strategy.blockSlot(parentRoot)).thenReturn(Optional.of(gloasSlot));
when(strategy.getAncestorNode(boostRoot, gloasSlot))
when(strategy.getAncestorNode(ForkChoiceNode.createBase(boostRoot), gloasSlot))
.thenReturn(
Optional.of(
new ForkChoiceNode(parentRoot, ForkChoicePayloadStatus.PAYLOAD_STATUS_FULL)));
Expand Down Expand Up @@ -553,12 +553,82 @@ void isParentStrong_shouldNotSubtractProposerBoostFromResolvedNodeWeight() {
when(strategy.getBlockData(parentRoot, ForkChoicePayloadStatus.PAYLOAD_STATUS_EMPTY))
.thenReturn(Optional.of(parentNode));
when(strategy.blockSlot(parentRoot)).thenReturn(Optional.of(gloasSlot));
when(strategy.getAncestorNode(parentRoot, gloasSlot))
when(strategy.getAncestorNode(ForkChoiceNode.createBase(parentRoot), gloasSlot))
.thenReturn(Optional.of(ForkChoiceNode.createBase(parentRoot)));

assertThat(forkChoiceUtil.isParentStrong(store, signedHead, proposerBoostAmount)).isTrue();
}

@Test
void isAncestor_returnsTrueWhenRootAndPayloadStatusMatch() {
final ReadOnlyForkChoiceStrategy strategy = mock(ReadOnlyForkChoiceStrategy.class);
final Bytes32 ancestorRoot = dataStructureUtil.randomBytes32();
final ForkChoiceNode node = ForkChoiceNode.createBase(dataStructureUtil.randomBytes32());
final ForkChoiceNode ancestor = ForkChoiceNode.createFull(ancestorRoot);

when(strategy.blockSlot(ancestorRoot)).thenReturn(Optional.of(gloasSlot));
when(strategy.getAncestorNode(node, gloasSlot))
.thenReturn(Optional.of(ForkChoiceNode.createFull(ancestorRoot)));

assertThat(forkChoiceUtil.isAncestor(strategy, node, ancestor)).isTrue();
}

@Test
void isAncestor_returnsTrueWhenAncestorIsPendingWildcard() {
final ReadOnlyForkChoiceStrategy strategy = mock(ReadOnlyForkChoiceStrategy.class);
final Bytes32 ancestorRoot = dataStructureUtil.randomBytes32();
final ForkChoiceNode node = ForkChoiceNode.createBase(dataStructureUtil.randomBytes32());
// A PENDING ancestor matches any resolved payload status for the same block root.
final ForkChoiceNode ancestor = ForkChoiceNode.createBase(ancestorRoot);

when(strategy.blockSlot(ancestorRoot)).thenReturn(Optional.of(gloasSlot));
when(strategy.getAncestorNode(node, gloasSlot))
.thenReturn(Optional.of(ForkChoiceNode.createFull(ancestorRoot)));

assertThat(forkChoiceUtil.isAncestor(strategy, node, ancestor)).isTrue();
}

@Test
void isAncestor_returnsFalseWhenPayloadStatusDiffersAndAncestorIsNotPending() {
final ReadOnlyForkChoiceStrategy strategy = mock(ReadOnlyForkChoiceStrategy.class);
final Bytes32 ancestorRoot = dataStructureUtil.randomBytes32();
final ForkChoiceNode node = ForkChoiceNode.createBase(dataStructureUtil.randomBytes32());
final ForkChoiceNode ancestor = ForkChoiceNode.createFull(ancestorRoot);

when(strategy.blockSlot(ancestorRoot)).thenReturn(Optional.of(gloasSlot));
when(strategy.getAncestorNode(node, gloasSlot))
.thenReturn(Optional.of(ForkChoiceNode.createEmpty(ancestorRoot)));

assertThat(forkChoiceUtil.isAncestor(strategy, node, ancestor)).isFalse();
}

@Test
void isAncestor_returnsFalseWhenResolvedRootDiffers() {
final ReadOnlyForkChoiceStrategy strategy = mock(ReadOnlyForkChoiceStrategy.class);
final Bytes32 ancestorRoot = dataStructureUtil.randomBytes32();
final ForkChoiceNode node = ForkChoiceNode.createBase(dataStructureUtil.randomBytes32());
final ForkChoiceNode ancestor = ForkChoiceNode.createBase(ancestorRoot);

when(strategy.blockSlot(ancestorRoot)).thenReturn(Optional.of(gloasSlot));
when(strategy.getAncestorNode(node, gloasSlot))
.thenReturn(Optional.of(ForkChoiceNode.createBase(dataStructureUtil.randomBytes32())));

assertThat(forkChoiceUtil.isAncestor(strategy, node, ancestor)).isFalse();
}

@Test
void isAncestor_returnsFalseWhenAncestorNodeCannotBeResolved() {
final ReadOnlyForkChoiceStrategy strategy = mock(ReadOnlyForkChoiceStrategy.class);
final Bytes32 ancestorRoot = dataStructureUtil.randomBytes32();
final ForkChoiceNode node = ForkChoiceNode.createBase(dataStructureUtil.randomBytes32());
final ForkChoiceNode ancestor = ForkChoiceNode.createBase(ancestorRoot);

when(strategy.blockSlot(ancestorRoot)).thenReturn(Optional.of(gloasSlot));
when(strategy.getAncestorNode(node, gloasSlot)).thenReturn(Optional.empty());

assertThat(forkChoiceUtil.isAncestor(strategy, node, ancestor)).isFalse();
}

// Helper methods to create blocks with specific properties
private BeaconBlock createBlockWithBlockHash(final Bytes32 blockHash) {
final BeaconBlock block = dataStructureUtil.randomBeaconBlock(gloasSlot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,12 @@ public Optional<Bytes32> getAncestor(final Bytes32 blockRoot, final UInt64 slot)
return Optional.empty();
}

@Override
public Optional<ForkChoiceNode> getAncestorNode(final ForkChoiceNode node, final UInt64 slot) {
// Base-only: this fixture does not model the Gloas three-state tree.
return getAncestor(node.blockRoot(), slot).map(ForkChoiceNode::createBase);
}

@Override
public Optional<ForkChoiceNode> getParentBeaconBlockNode(final ForkChoiceNode node) {
throw new UnsupportedOperationException("Not implemented");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ public Optional<Bytes32> getAncestor(final Bytes32 blockRoot, final UInt64 slot)
return getBlock(slot).map(SignedBeaconBlock::getRoot);
}

@Override
public Optional<ForkChoiceNode> getAncestorNode(final ForkChoiceNode node, final UInt64 slot) {
// This fixture models only the pre-Gloas (single-variant) tree, so ancestry is resolved by
// block root and always yields base (PENDING) nodes.
return getAncestor(node.blockRoot(), slot).map(ForkChoiceNode::createBase);
}

@Override
public Optional<ForkChoiceNode> getParentBeaconBlockNode(final ForkChoiceNode node) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ private Optional<Bytes32> getDependentRoot(
return Optional.of(Bytes32.ZERO);
}
return forkChoiceStrategy
.getAncestorNode(node.blockRoot(), maybeDependentSlot.get())
.getAncestorNode(node, maybeDependentSlot.get())
.map(ForkChoiceNode::blockRoot);
}

Expand Down
7 changes: 0 additions & 7 deletions specrefs/.ethspecify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,6 @@ specrefs:
- get_proposer_dependent_root#gloas
- get_proposer_head#gloas

# Redesign fork choice abstractions https://github.com/Consensys/teku/issues/10773
- get_supported_node#phase0
- get_supported_node#gloas
- is_ancestor#phase0
- is_ancestor#gloas
- is_epoch_boundary#phase0

# Not implemented: heze
- get_forkchoice_store#heze
- get_inclusion_list_committee#heze
Expand Down
Loading
Loading