-
Notifications
You must be signed in to change notification settings - Fork 1k
Check the bad block manager when receiving a new block from the network #10212
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
Open
daniellehrner
wants to merge
5
commits into
besu-eth:main
Choose a base branch
from
daniellehrner:fix/avoid_bad_ancestor_endless_loop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
28962b5
check the bad block manager when receiving a new block from the netwo…
daniellehrner ca65d08
changed log level, improved comments in tests
daniellehrner e8084f9
changed log level to debug for not important events
daniellehrner 14f21de
addressed pr comments
daniellehrner 9d8add1
make tests stricter
daniellehrner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
235 changes: 235 additions & 0 deletions
235
...pi/jsonrpc/internal/methods/engine/EngineForkchoiceUpdatedBadAncestorIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| /* | ||
| * Copyright contributors to Besu. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine; | ||
|
|
||
| import static java.util.Collections.emptyList; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.hyperledger.besu.datatypes.HardforkId.MainnetHardforkId.AMSTERDAM; | ||
| import static org.hyperledger.besu.datatypes.HardforkId.MainnetHardforkId.CANCUN; | ||
| import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.ExecutionEngineJsonRpcMethod.EngineStatus.INVALID; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import org.hyperledger.besu.consensus.merge.MergeContext; | ||
| import org.hyperledger.besu.consensus.merge.blockcreation.MergeCoordinator; | ||
| import org.hyperledger.besu.datatypes.Hash; | ||
| import org.hyperledger.besu.ethereum.ProtocolContext; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EngineForkchoiceUpdatedParameter; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.EngineUpdateForkchoiceResult; | ||
| import org.hyperledger.besu.ethereum.chain.BadBlockCause; | ||
| import org.hyperledger.besu.ethereum.chain.BadBlockManager; | ||
| import org.hyperledger.besu.ethereum.chain.MutableBlockchain; | ||
| import org.hyperledger.besu.ethereum.core.Block; | ||
| import org.hyperledger.besu.ethereum.core.BlockBody; | ||
| import org.hyperledger.besu.ethereum.core.BlockHeader; | ||
| import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; | ||
| import org.hyperledger.besu.ethereum.core.MiningConfiguration; | ||
| import org.hyperledger.besu.ethereum.eth.manager.EthScheduler; | ||
| import org.hyperledger.besu.ethereum.eth.sync.backwardsync.BackwardSyncContext; | ||
| import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; | ||
| import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; | ||
| import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import io.vertx.core.Vertx; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Integration test for the engine_forkchoiceUpdated "invalid block as ancestor" flow. | ||
| * | ||
| * <ol> | ||
| * <li>A bad block B is registered in {@link BadBlockManager} (simulating what {@link | ||
| * org.hyperledger.besu.ethereum.MainnetBlockValidator} does on a failed import). | ||
| * <li>{@link MergeCoordinator#onBadChain(Block, List, List)} is called with B and a descendant D | ||
| * simulating the backward-sync code path that fires on {@code | ||
| * BackwardSyncContext.emitBadChainEvent}. This should propagate B's "bad" marking to every | ||
| * descendant, and record each descendant's {@code latestValidHash} as B's (valid) parent | ||
| * hash. | ||
| * <li>A JSON-RPC {@code engine_forkchoiceUpdated} call is invoked with {@code headBlockHash = D}. | ||
| * The bad-block short-circuit in {@link | ||
| * org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.AbstractEngineForkchoiceUpdated} | ||
| * should fire, returning {@code INVALID} with {@code latestValidHash} equal to B's valid | ||
| * parent hash. | ||
| * </ol> | ||
| */ | ||
| public class EngineForkchoiceUpdatedBadAncestorIntegrationTest { | ||
|
|
||
| private static final Vertx vertx = Vertx.vertx(); | ||
|
|
||
| @AfterAll | ||
| public static void tearDown() { | ||
| vertx.close(); | ||
| } | ||
|
|
||
| private final BlockHeaderTestFixture headerBuilder = new BlockHeaderTestFixture(); | ||
|
|
||
| private BadBlockManager badBlockManager; | ||
| private MutableBlockchain blockchain; | ||
| private MergeCoordinator mergeCoordinator; | ||
| private EngineForkchoiceUpdatedV3 method; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| badBlockManager = new BadBlockManager(); | ||
|
|
||
| // Blockchain only needs to serve the bad block's parent header lookup for | ||
| // MergeCoordinator.onBadChain; everything else on the fcU short-circuit path avoids it. | ||
| blockchain = mock(MutableBlockchain.class); | ||
|
|
||
| final MergeContext mergeContext = mock(MergeContext.class); | ||
| when(mergeContext.as(MergeContext.class)).thenReturn(mergeContext); | ||
|
|
||
| final WorldStateArchive worldStateArchive = mock(WorldStateArchive.class); | ||
|
|
||
| final ProtocolContext protocolContext = | ||
| new ProtocolContext.Builder() | ||
| .withBlockchain(blockchain) | ||
| .withWorldStateArchive(worldStateArchive) | ||
| .withConsensusContext(mergeContext) | ||
| .withBadBlockManager(badBlockManager) | ||
| .build(); | ||
|
|
||
| final ProtocolSchedule protocolSchedule = mock(ProtocolSchedule.class); | ||
|
|
||
| when(protocolSchedule.getChainId()).thenReturn(Optional.empty()); | ||
| when(protocolSchedule.milestoneFor(CANCUN)).thenReturn(Optional.empty()); | ||
| when(protocolSchedule.milestoneFor(AMSTERDAM)).thenReturn(Optional.empty()); | ||
|
|
||
| final EthScheduler ethScheduler = mock(EthScheduler.class); | ||
| final TransactionPool transactionPool = mock(TransactionPool.class); | ||
| final BackwardSyncContext backwardSyncContext = mock(BackwardSyncContext.class); | ||
| final MiningConfiguration miningConfiguration = MiningConfiguration.newDefault(); | ||
|
|
||
| mergeCoordinator = | ||
| new MergeCoordinator( | ||
| protocolContext, | ||
| protocolSchedule, | ||
| ethScheduler, | ||
| transactionPool, | ||
| miningConfiguration, | ||
| backwardSyncContext); | ||
|
|
||
| method = | ||
| new EngineForkchoiceUpdatedV3( | ||
| vertx, | ||
| protocolSchedule, | ||
| protocolContext, | ||
| mergeCoordinator, | ||
| mock(EngineCallListener.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnInvalidForForkchoiceUpdateWhenHeadHasBadAncestor() { | ||
| // Chain shape: | ||
| // validParent (PoS, difficulty=0, known to the blockchain) | ||
| // └── badBlock B (rejected by validator, in BadBlockManager) | ||
| // └── descendant D (CL's head — only in BadBlockManager via onBadChain) | ||
| final BlockHeader validParent = headerBuilder.number(100L).buildHeader(); | ||
| final BlockHeader badHeader = | ||
| headerBuilder.number(101L).parentHash(validParent.getHash()).buildHeader(); | ||
| final Block badBlock = new Block(badHeader, BlockBody.empty()); | ||
| final BlockHeader descendantHeader = | ||
| headerBuilder.number(102L).parentHash(badHeader.getHash()).buildHeader(); | ||
|
|
||
| // onBadChain looks up the bad block's parent in the blockchain to compute latestValidHash. | ||
| when(blockchain.getBlockHeader(validParent.getHash())).thenReturn(Optional.of(validParent)); | ||
|
|
||
| // Simulate MainnetBlockValidator.handleFailedBlockProcessing — add the bad block | ||
| // to the manager directly. (The direct add path intentionally does NOT record a | ||
| // latestValidHash for B itself; only the descendants in stage 2 get one.) | ||
| badBlockManager.addBadBlock(badBlock, BadBlockCause.fromValidationFailure("BAL mismatch")); | ||
|
|
||
| // Simulate BackwardSyncContext.emitBadChainEvent firing — MergeCoordinator is | ||
| // registered as a BadChainListener in its constructor and receives the descendant list. | ||
| mergeCoordinator.onBadChain(badBlock, emptyList(), List.of(descendantHeader)); | ||
|
|
||
| // onBadChain propagated "bad" status and latestValidHash to the descendant. | ||
| assertThat(mergeCoordinator.isBadBlock(descendantHeader.getHash())).isTrue(); | ||
| assertThat(mergeCoordinator.getLatestValidHashOfBadBlock(descendantHeader.getHash())) | ||
| .contains(validParent.getHash()); | ||
|
|
||
| // CL sends engine_forkchoiceUpdated with the descendant as the head, simulating the | ||
| // case where the CL reuses a head whose ancestry was poisoned while we were backward-syncing. | ||
| final JsonRpcResponse response = | ||
| invokeForkchoiceUpdated( | ||
| new EngineForkchoiceUpdatedParameter( | ||
| descendantHeader.getHash(), validParent.getHash(), validParent.getHash())); | ||
|
|
||
| // Must return INVALID with | ||
| // latestValidHash pointing at the last valid ancestor (B's parent), not SYNCING and not | ||
| // VALID, without ever touching the blockchain state. | ||
| final EngineUpdateForkchoiceResult forkchoiceResult = | ||
| (EngineUpdateForkchoiceResult) ((JsonRpcSuccessResponse) response).getResult(); | ||
| assertThat(forkchoiceResult.getPayloadStatus().getStatus()).isEqualTo(INVALID); | ||
| assertThat(forkchoiceResult.getPayloadStatus().getLatestValidHashAsString()) | ||
| .isEqualTo(validParent.getHash().toHexString()); | ||
| final String error = forkchoiceResult.getPayloadStatus().getError(); | ||
| assertThat(error).contains(descendantHeader.getHash().toString()); | ||
| assertThat(error).containsIgnoringCase("invalid"); | ||
| assertThat(forkchoiceResult.getPayloadId()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnInvalidWithZeroHashWhenBadBlockParentIsUnknown() { | ||
| // Same shape as the previous test, except the bad block's parent is NOT in the blockchain. | ||
| // This models a deep backward-sync failure where we never resolved the ancestor — onBadChain | ||
| // cannot compute a latestValidHash, so the fcU short-circuit must fall back to Hash.ZERO. | ||
| final BlockHeader unknownParent = headerBuilder.number(100L).buildHeader(); | ||
| final BlockHeader badHeader = | ||
| headerBuilder.number(101L).parentHash(unknownParent.getHash()).buildHeader(); | ||
| final Block badBlock = new Block(badHeader, BlockBody.empty()); | ||
| final BlockHeader descendantHeader = | ||
| headerBuilder.number(102L).parentHash(badHeader.getHash()).buildHeader(); | ||
|
|
||
| // Deliberately leave blockchain.getBlockHeader(unknownParent.getHash()) unstubbed so the | ||
| // mock returns Optional.empty() — that's the path that drives maybeLatestValidHash empty. | ||
|
|
||
| badBlockManager.addBadBlock(badBlock, BadBlockCause.fromValidationFailure("BAL mismatch")); | ||
| mergeCoordinator.onBadChain(badBlock, emptyList(), List.of(descendantHeader)); | ||
|
|
||
| // Descendant is still marked bad, but no latestValidHash was recorded for it. | ||
| assertThat(mergeCoordinator.isBadBlock(descendantHeader.getHash())).isTrue(); | ||
| assertThat(mergeCoordinator.getLatestValidHashOfBadBlock(descendantHeader.getHash())).isEmpty(); | ||
|
|
||
| final JsonRpcResponse response = | ||
| invokeForkchoiceUpdated( | ||
| new EngineForkchoiceUpdatedParameter( | ||
| descendantHeader.getHash(), unknownParent.getHash(), unknownParent.getHash())); | ||
|
|
||
| final EngineUpdateForkchoiceResult forkchoiceResult = | ||
| (EngineUpdateForkchoiceResult) ((JsonRpcSuccessResponse) response).getResult(); | ||
| assertThat(forkchoiceResult.getPayloadStatus().getStatus()).isEqualTo(INVALID); | ||
| assertThat(forkchoiceResult.getPayloadStatus().getLatestValidHashAsString()) | ||
| .isEqualTo(Hash.ZERO.toHexString()); | ||
| final String error = forkchoiceResult.getPayloadStatus().getError(); | ||
| assertThat(error).contains(descendantHeader.getHash().toString()); | ||
| assertThat(error).containsIgnoringCase("invalid"); | ||
| assertThat(forkchoiceResult.getPayloadId()).isNull(); | ||
| } | ||
|
|
||
| private JsonRpcResponse invokeForkchoiceUpdated(final EngineForkchoiceUpdatedParameter fcu) { | ||
| return method.response( | ||
| new JsonRpcRequestContext( | ||
| new JsonRpcRequest("2.0", "engine_forkchoiceUpdatedV3", new Object[] {fcu}))); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.