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

3943 stop blocks on finalized #4058

Merged
merged 9 commits into from
Jul 11, 2022
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 @@ -49,6 +49,7 @@
import org.hyperledger.besu.ethereum.eth.manager.EthPeers;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager;
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
import org.hyperledger.besu.ethereum.eth.manager.MergePeerFilter;
import org.hyperledger.besu.ethereum.eth.manager.snap.SnapProtocolManager;
import org.hyperledger.besu.ethereum.eth.peervalidation.CheckpointBlocksPeerValidator;
import org.hyperledger.besu.ethereum.eth.peervalidation.ClassicForkPeerValidator;
Expand Down Expand Up @@ -401,7 +402,8 @@ public BesuController build() {
ethContext,
ethMessages,
scheduler,
peerValidators);
peerValidators,
Optional.empty());

final Optional<SnapProtocolManager> maybeSnapProtocolManager =
createSnapProtocolManager(peerValidators, ethPeers, snapMessages, worldStateArchive);
Expand Down Expand Up @@ -559,7 +561,8 @@ protected EthProtocolManager createEthProtocolManager(
final EthContext ethContext,
final EthMessages ethMessages,
final EthScheduler scheduler,
final List<PeerValidator> peerValidators) {
final List<PeerValidator> peerValidators,
final Optional<MergePeerFilter> mergePeerFilter) {
return new EthProtocolManager(
protocolContext.getBlockchain(),
networkId,
Expand All @@ -570,6 +573,7 @@ protected EthProtocolManager createEthProtocolManager(
ethMessages,
ethContext,
peerValidators,
mergePeerFilter,
fastSyncEnabled,
scheduler,
genesisConfig.getForks());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.hyperledger.besu.ethereum.eth.manager.EthPeers;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager;
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
import org.hyperledger.besu.ethereum.eth.manager.MergePeerFilter;
import org.hyperledger.besu.ethereum.eth.manager.snap.SnapProtocolManager;
import org.hyperledger.besu.ethereum.eth.peervalidation.PeerValidator;
import org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration;
Expand Down Expand Up @@ -232,7 +233,8 @@ protected EthProtocolManager createEthProtocolManager(
final EthContext ethContext,
final EthMessages ethMessages,
final EthScheduler scheduler,
final List<PeerValidator> peerValidators) {
final List<PeerValidator> peerValidators,
final Optional<MergePeerFilter> mergePeerFilter) {
return besuControllerBuilderSchedule
.get(0L)
.createEthProtocolManager(
Expand All @@ -244,7 +246,8 @@ protected EthProtocolManager createEthProtocolManager(
ethContext,
ethMessages,
scheduler,
peerValidators);
peerValidators,
mergePeerFilter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.hyperledger.besu.ethereum.eth.manager.EthPeers;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager;
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
import org.hyperledger.besu.ethereum.eth.manager.MergePeerFilter;
import org.hyperledger.besu.ethereum.eth.manager.snap.SnapProtocolManager;
import org.hyperledger.besu.ethereum.eth.peervalidation.PeerValidator;
import org.hyperledger.besu.ethereum.eth.sync.state.SyncState;
Expand Down Expand Up @@ -125,7 +126,8 @@ protected EthProtocolManager createEthProtocolManager(
final EthContext ethContext,
final EthMessages ethMessages,
final EthScheduler scheduler,
final List<PeerValidator> peerValidators) {
final List<PeerValidator> peerValidators,
final Optional<MergePeerFilter> mergePeerFilter) {
LOG.info("Operating on IBFT-1.0 network.");
return new Istanbul99ProtocolManager(
protocolContext.getBlockchain(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.MiningParameters;
import org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.manager.EthMessages;
import org.hyperledger.besu.ethereum.eth.manager.EthPeers;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager;
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
import org.hyperledger.besu.ethereum.eth.manager.MergePeerFilter;
import org.hyperledger.besu.ethereum.eth.peervalidation.PeerValidator;
import org.hyperledger.besu.ethereum.eth.peervalidation.RequiredBlocksPeerValidator;
import org.hyperledger.besu.ethereum.eth.sync.backwardsync.BackwardChain;
Expand Down Expand Up @@ -62,7 +68,6 @@ protected MiningCoordinator createMiningCoordinator(
transactionPool,
miningParameters,
syncState,
ethProtocolManager,
new BackwardSyncContext(
protocolContext,
protocolSchedule,
Expand All @@ -73,13 +78,50 @@ protected MiningCoordinator createMiningCoordinator(
storageProvider, ScheduleBasedBlockHeaderFunctions.create(protocolSchedule))));
}

@Override
protected EthProtocolManager createEthProtocolManager(
final ProtocolContext protocolContext,
final boolean fastSyncEnabled,
final TransactionPool transactionPool,
final EthProtocolConfiguration ethereumWireProtocolConfiguration,
final EthPeers ethPeers,
final EthContext ethContext,
final EthMessages ethMessages,
final EthScheduler scheduler,
final List<PeerValidator> peerValidators,
final Optional<MergePeerFilter> mergePeerFilter) {

if (mergePeerFilter.isPresent()) {
protocolContext
.getConsensusContext(MergeContext.class)
.observeNewIsPostMergeState(mergePeerFilter.get());
protocolContext
.getConsensusContext(MergeContext.class)
.addNewForkchoiceMessageListener(mergePeerFilter.get());
}

EthProtocolManager ethProtocolManager =
super.createEthProtocolManager(
protocolContext,
fastSyncEnabled,
transactionPool,
ethereumWireProtocolConfiguration,
ethPeers,
ethContext,
ethMessages,
scheduler,
peerValidators,
mergePeerFilter);

return ethProtocolManager;
}

protected MiningCoordinator createTransitionMiningCoordinator(
final ProtocolSchedule protocolSchedule,
final ProtocolContext protocolContext,
final TransactionPool transactionPool,
final MiningParameters miningParameters,
final SyncState syncState,
final EthProtocolManager ethProtocolManager,
final BackwardSyncContext backwardSyncContext) {

this.syncState.set(syncState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ protected MiningCoordinator createMiningCoordinator(
transactionPool,
transitionMiningParameters,
syncState,
ethProtocolManager,
transitionBackwardsSyncContext));
initTransitionWatcher(protocolContext, composedCoordinator);
return composedCoordinator;
Expand Down Expand Up @@ -150,8 +149,8 @@ private void initTransitionWatcher(

PostMergeContext postMergeContext = protocolContext.getConsensusContext(PostMergeContext.class);
postMergeContext.observeNewIsPostMergeState(
newIsPostMergeState -> {
if (newIsPostMergeState) {
(isPoS, difficultyStoppedAt) -> {
if (isPoS) {
// if we transitioned to post-merge, stop and disable any mining
composedCoordinator.getPreMergeObject().disable();
composedCoordinator.getPreMergeObject().stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.math.BigInteger;
import java.util.List;
import java.util.Optional;

/** This allows for interoperability with Quorum, but shouldn't be used otherwise. */
public class Istanbul99ProtocolManager extends EthProtocolManager {
Expand All @@ -56,6 +57,7 @@ public Istanbul99ProtocolManager(
ethMessages,
ethContext,
peerValidators,
Optional.empty(),
fastSyncEnabled,
scheduler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package org.hyperledger.besu.consensus.merge;

import org.hyperledger.besu.consensus.merge.MergeContext.NewForkchoiceMessageListener;
import org.hyperledger.besu.datatypes.Hash;

import java.util.Optional;
Expand All @@ -24,7 +23,7 @@
import org.slf4j.LoggerFactory;

public class FinalizedBlockHashSupplier
implements Supplier<Optional<Hash>>, NewForkchoiceMessageListener {
implements Supplier<Optional<Hash>>, ForkchoiceMessageListener {
private static final Logger LOG = LoggerFactory.getLogger(FinalizedBlockHashSupplier.class);

private volatile Optional<Hash> lastAnnouncedFinalizedBlockHash = Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ public interface MergeContext extends ConsensusContext {

boolean isSyncing();

void observeNewIsPostMergeState(final NewMergeStateCallback newMergeStateCallback);
void observeNewIsPostMergeState(final MergeStateHandler mergeStateHandler);

long addNewForkchoiceMessageListener(
final NewForkchoiceMessageListener newForkchoiceMessageListener);
long addNewForkchoiceMessageListener(final ForkchoiceMessageListener forkchoiceMessageListener);

void removeNewForkchoiceMessageListener(final long subscriberId);

Expand Down Expand Up @@ -67,15 +66,4 @@ void fireNewUnverifiedForkchoiceMessageEvent(
final Hash headBlockHash,
final Optional<Hash> maybeFinalizedBlockHash,
final Hash safeBlockHash);

interface NewMergeStateCallback {
void onNewIsPostMergeState(final boolean newIsPostMergeState);
}

interface NewForkchoiceMessageListener {
void onNewForkchoiceMessage(
final Hash headBlockHash,
final Optional<Hash> maybeFinalizedBlockHash,
final Hash safeBlockHash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public class PostMergeContext implements MergeContext {
// initial postMerge state is indeterminate until it is set:
private final AtomicReference<Optional<Boolean>> isPostMerge =
new AtomicReference<>(Optional.empty());
private final Subscribers<NewMergeStateCallback> newMergeStateCallbackSubscribers =
private final Subscribers<MergeStateHandler> newMergeStateCallbackSubscribers =
Subscribers.create();
private final Subscribers<NewForkchoiceMessageListener> newForkchoiceMessageCallbackSubscribers =
private final Subscribers<ForkchoiceMessageListener> newForkchoiceMessageCallbackSubscribers =
Subscribers.create();

private final EvictingQueue<PayloadTuple> blocksInProgress =
Expand Down Expand Up @@ -99,7 +99,8 @@ public void setIsPostMerge(final Difficulty totalDifficulty) {

if (oldState.isEmpty() || oldState.get() != newState) {
newMergeStateCallbackSubscribers.forEach(
newMergeStateCallback -> newMergeStateCallback.onNewIsPostMergeState(newState));
newMergeStateCallback ->
newMergeStateCallback.mergeStateChanged(newState, Optional.of(totalDifficulty)));
}
}

Expand All @@ -123,14 +124,14 @@ public boolean isSyncing() {
}

@Override
public void observeNewIsPostMergeState(final NewMergeStateCallback newMergeStateCallback) {
newMergeStateCallbackSubscribers.subscribe(newMergeStateCallback);
public void observeNewIsPostMergeState(final MergeStateHandler mergeStateHandler) {
newMergeStateCallbackSubscribers.subscribe(mergeStateHandler);
}

@Override
public long addNewForkchoiceMessageListener(
final NewForkchoiceMessageListener newForkchoiceMessageListener) {
return newForkchoiceMessageCallbackSubscribers.subscribe(newForkchoiceMessageListener);
final ForkchoiceMessageListener forkchoiceMessageListener) {
return newForkchoiceMessageCallbackSubscribers.subscribe(forkchoiceMessageListener);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ public boolean isSyncing() {
}

@Override
public void observeNewIsPostMergeState(final NewMergeStateCallback newMergeStateCallback) {
postMergeContext.observeNewIsPostMergeState(newMergeStateCallback);
public void observeNewIsPostMergeState(final MergeStateHandler mergeStateHandler) {
postMergeContext.observeNewIsPostMergeState(mergeStateHandler);
}

@Override
public long addNewForkchoiceMessageListener(
final NewForkchoiceMessageListener newForkchoiceMessageListener) {
return postMergeContext.addNewForkchoiceMessageListener(newForkchoiceMessageListener);
final ForkchoiceMessageListener forkchoiceMessageListener) {
return postMergeContext.addNewForkchoiceMessageListener(forkchoiceMessageListener);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.consensus.merge.MergeContext.NewMergeStateCallback;
import org.hyperledger.besu.consensus.merge.blockcreation.PayloadIdentifier;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockHeader;
Expand Down Expand Up @@ -169,12 +168,13 @@ public void tryingToRetrieveABlockPutButEvictedReturnsEmpty() {
assertThat(postMergeContext.retrieveBlockById(evictedPayloadId)).isEmpty();
}

private static class MergeStateChangeCollector implements NewMergeStateCallback {
private static class MergeStateChangeCollector implements MergeStateHandler {
final List<Boolean> stateChanges = new ArrayList<>();

@Override
public void onNewIsPostMergeState(final boolean newIsPostMergeState) {
stateChanges.add(newIsPostMergeState);
public void mergeStateChanged(
final boolean isPoS, final Optional<Difficulty> difficultyStoppedAt) {
stateChanges.add(isPoS);
}

public void reset() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* 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.consensus.merge;

import org.hyperledger.besu.datatypes.Hash;

import java.util.Optional;

public interface ForkchoiceMessageListener {

void onNewForkchoiceMessage(
final Hash headBlockHash,

Check notice

Code scanning / CodeQL

Useless parameter

The parameter headBlockHash is unused.
final Optional<Hash> maybeFinalizedBlockHash,
final Hash safeBlockHash);

Check notice

Code scanning / CodeQL

Useless parameter

The parameter safeBlockHash is unused.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* 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.consensus.merge;

import org.hyperledger.besu.ethereum.core.Difficulty;

import java.util.Optional;

public interface MergeStateHandler {

void mergeStateChanged(final boolean isPoS, final Optional<Difficulty> difficultyStoppedAt);
}
Loading