Skip to content

Commit

Permalink
Merge branch 'hyperledger:main' into zkbesu
Browse files Browse the repository at this point in the history
  • Loading branch information
delehef authored Feb 14, 2024
2 parents dd4fc9c + 61c7e18 commit 9d276ae
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 14 deletions.
8 changes: 6 additions & 2 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,12 @@
synopsisHeading = "%n",
descriptionHeading = "%n@|bold,fg(cyan) Description:|@%n%n",
optionListHeading = "%n@|bold,fg(cyan) Options:|@%n",
footerHeading = "%n",
footer = "Besu is licensed under the Apache License 2.0")
footerHeading = "%nBesu is licensed under the Apache License 2.0%n",
footer = {
"%n%n@|fg(cyan) To get started quickly, just choose a network to sync and a profile to run with suggested defaults:|@",
"%n@|fg(cyan) for Mainnet|@ --network=mainnet --profile=[minimalist_staker|staker]",
"%nMore info and other profiles at https://besu.hyperledger.org%n"
})
public class BesuCommand implements DefaultCommandValues, Runnable {

@SuppressWarnings("PrivateStaticFinalLoggers")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ public void noOverrideDefaultValuesIfKeyIsNotPresentInConfigFile() {
assertThat(syncConfig.getSyncMode()).isEqualTo(SyncMode.FAST);
assertThat(syncConfig.getFastSyncMinimumPeerCount()).isEqualTo(5);

assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();

assertThat(commandOutput.toString(UTF_8)).isEmpty();
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}
Expand Down
37 changes: 37 additions & 0 deletions besu/src/test/java/org/hyperledger/besu/cli/ProfilesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.cli;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;

import org.hyperledger.besu.cli.config.ProfileName;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

public class ProfilesTest extends CommandTestAbstract {

/** Test if besu will validate the combination of options within the given profile. */
@ParameterizedTest
@EnumSource(ProfileName.class)
public void testProfileWithNoOverrides_doesNotError(final ProfileName profileName) {

parseCommand("--profile", profileName.name());

assertThat(commandOutput.toString(UTF_8)).isEmpty();
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}
}
2 changes: 1 addition & 1 deletion config/src/main/resources/profiles/minimalist-staker.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sync-mode="X_CHECKPOINT"
sync-mode="CHECKPOINT"
data-storage-format="BONSAI"
bonsai-historical-block-limit=128
max-peers=25
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.consensus.ibft.validation.FutureRoundProposalMessageValidator;
import org.hyperledger.besu.consensus.ibft.validation.MessageValidatorFactory;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.plugin.services.securitymodule.SecurityModuleException;

Expand Down Expand Up @@ -122,6 +123,9 @@ public IbftBlockHeightManager(

@Override
public void handleBlockTimerExpiry(final ConsensusRoundIdentifier roundIdentifier) {

logValidatorChanges(currentRound);

if (roundIdentifier.equals(currentRound.getRoundIdentifier())) {
final long headerTimeStampSeconds = Math.round(clock.millis() / 1000D);
currentRound.createAndSendProposalMessage(headerTimeStampSeconds);
Expand All @@ -133,6 +137,30 @@ public void handleBlockTimerExpiry(final ConsensusRoundIdentifier roundIdentifie
}
}

/**
* If the list of validators for the next block to be proposed/imported has changed from the
* previous block, log the change. Only log for round 0 (i.e. once per block).
*
* @param ibftRound The current round
*/
private void logValidatorChanges(final IbftRound ibftRound) {
if (ibftRound.getRoundIdentifier().getRoundNumber() == 0) {
final Collection<Address> previousValidators =
MessageValidatorFactory.getValidatorsForBlock(ibftRound.protocolContext, parentHeader);
final Collection<Address> validatorsForHeight =
MessageValidatorFactory.getValidatorsAfterBlock(ibftRound.protocolContext, parentHeader);
if (!(validatorsForHeight.containsAll(previousValidators))
|| !(previousValidators.containsAll(validatorsForHeight))) {
LOG.info(
"Validator list change. Previous chain height {}: {}. Current chain height {}: {}.",
parentHeader.getNumber(),
previousValidators,
parentHeader.getNumber() + 1,
validatorsForHeight);
}
}
}

@Override
public void roundExpired(final RoundExpiry expire) {
if (!expire.getView().equals(currentRound.getRoundIdentifier())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public class IbftRound {
private final Subscribers<MinedBlockObserver> observers;
private final RoundState roundState;
private final BlockCreator blockCreator;
private final ProtocolContext protocolContext;
/** The protocol context. */
protected final ProtocolContext protocolContext;

private final ProtocolSchedule protocolSchedule;
private final NodeKey nodeKey;
private final MessageFactory messageFactory; // used only to create stored local msgs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,41 @@ public MessageValidatorFactory(
this.bftExtraDataCodec = bftExtraDataCodec;
}

private Collection<Address> getValidatorsAfterBlock(final BlockHeader parentHeader) {
/**
* Get the list of validators that are applicable after the given block
*
* @param protocolContext the protocol context
* @param parentHeader the parent header
* @return the list of validators
*/
public static Collection<Address> getValidatorsAfterBlock(
final ProtocolContext protocolContext, final BlockHeader parentHeader) {
return protocolContext
.getConsensusContext(BftContext.class)
.getValidatorProvider()
.getValidatorsAfterBlock(parentHeader);
}

/**
* Get the list of validators that are applicable for the given block
*
* @param protocolContext the protocol context
* @param parentHeader the parent header
* @return the list of validators
*/
public static Collection<Address> getValidatorsForBlock(
final ProtocolContext protocolContext, final BlockHeader parentHeader) {
return protocolContext
.getConsensusContext(BftContext.class)
.getValidatorProvider()
.getValidatorsForBlock(parentHeader);
}

private SignedDataValidator createSignedDataValidator(
final ConsensusRoundIdentifier roundIdentifier, final BlockHeader parentHeader) {

return new SignedDataValidator(
getValidatorsAfterBlock(parentHeader),
getValidatorsAfterBlock(protocolContext, parentHeader),
proposerSelector.selectProposerForRound(roundIdentifier),
roundIdentifier);
}
Expand All @@ -79,7 +102,7 @@ private SignedDataValidator createSignedDataValidator(
*/
public MessageValidator createMessageValidator(
final ConsensusRoundIdentifier roundIdentifier, final BlockHeader parentHeader) {
final Collection<Address> validators = getValidatorsAfterBlock(parentHeader);
final Collection<Address> validators = getValidatorsAfterBlock(protocolContext, parentHeader);

final BftBlockInterface bftBlockInterface =
protocolContext.getConsensusContext(BftContext.class).getBlockInterface();
Expand All @@ -106,7 +129,7 @@ public MessageValidator createMessageValidator(
*/
public RoundChangeMessageValidator createRoundChangeMessageValidator(
final long chainHeight, final BlockHeader parentHeader) {
final Collection<Address> validators = getValidatorsAfterBlock(parentHeader);
final Collection<Address> validators = getValidatorsAfterBlock(protocolContext, parentHeader);

final BftBlockInterface bftBlockInterface =
protocolContext.getConsensusContext(BftContext.class).getBlockInterface();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.hyperledger.besu.consensus.qbft.payload.MessageFactory;
import org.hyperledger.besu.consensus.qbft.validation.FutureRoundProposalMessageValidator;
import org.hyperledger.besu.consensus.qbft.validation.MessageValidatorFactory;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.plugin.services.securitymodule.SecurityModuleException;

Expand Down Expand Up @@ -126,6 +127,9 @@ public void handleBlockTimerExpiry(final ConsensusRoundIdentifier roundIdentifie
startNewRound(0);

final QbftRound qbftRound = currentRound.get();

logValidatorChanges(qbftRound);

// mining will be checked against round 0 as the current round is initialised to 0 above
final boolean isProposer =
finalState.isLocalNodeProposerForRound(qbftRound.getRoundIdentifier());
Expand All @@ -143,6 +147,30 @@ public void handleBlockTimerExpiry(final ConsensusRoundIdentifier roundIdentifie
}
}

/**
* If the list of validators for the next block to be proposed/imported has changed from the
* previous block, log the change. Only log for round 0 (i.e. once per block).
*
* @param qbftRound The current round
*/
private void logValidatorChanges(final QbftRound qbftRound) {
if (qbftRound.getRoundIdentifier().getRoundNumber() == 0) {
final Collection<Address> previousValidators =
MessageValidatorFactory.getValidatorsForBlock(qbftRound.protocolContext, parentHeader);
final Collection<Address> validatorsForHeight =
MessageValidatorFactory.getValidatorsAfterBlock(qbftRound.protocolContext, parentHeader);
if (!(validatorsForHeight.containsAll(previousValidators))
|| !(previousValidators.containsAll(validatorsForHeight))) {
LOG.info(
"Validator list change. Previous chain height {}: {}. Current chain height {}: {}.",
parentHeader.getNumber(),
previousValidators,
parentHeader.getNumber() + 1,
validatorsForHeight);
}
}
}

@Override
public void roundExpired(final RoundExpiry expire) {
if (currentRound.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,36 @@ public MessageValidatorFactory(
this.bftExtraDataCodec = bftExtraDataCodec;
}

private Collection<Address> getValidatorsAfterBlock(final BlockHeader parentHeader) {
/**
* Get the list of validators that are applicable after the given block
*
* @param protocolContext the protocol context
* @param parentHeader the parent header
* @return the list of validators
*/
public static Collection<Address> getValidatorsAfterBlock(
final ProtocolContext protocolContext, final BlockHeader parentHeader) {
return protocolContext
.getConsensusContext(BftContext.class)
.getValidatorProvider()
.getValidatorsAfterBlock(parentHeader);
}

/**
* Get the list of validators that are applicable for the given block
*
* @param protocolContext the protocol context
* @param parentHeader the parent header
* @return the list of validators
*/
public static Collection<Address> getValidatorsForBlock(
final ProtocolContext protocolContext, final BlockHeader parentHeader) {
return protocolContext
.getConsensusContext(BftContext.class)
.getValidatorProvider()
.getValidatorsForBlock(parentHeader);
}

/**
* Create round change message validator.
*
Expand All @@ -72,7 +95,8 @@ private Collection<Address> getValidatorsAfterBlock(final BlockHeader parentHead
public RoundChangeMessageValidator createRoundChangeMessageValidator(
final long chainHeight, final BlockHeader parentHeader) {

final Collection<Address> validatorsForHeight = getValidatorsAfterBlock(parentHeader);
final Collection<Address> validatorsForHeight =
getValidatorsAfterBlock(protocolContext, parentHeader);

final RoundChangePayloadValidator roundChangePayloadValidator =
new RoundChangePayloadValidator(validatorsForHeight, chainHeight);
Expand All @@ -95,8 +119,8 @@ public RoundChangeMessageValidator createRoundChangeMessageValidator(
*/
public MessageValidator createMessageValidator(
final ConsensusRoundIdentifier roundIdentifier, final BlockHeader parentHeader) {

final Collection<Address> validatorsForHeight = getValidatorsAfterBlock(parentHeader);
final Collection<Address> validatorsForHeight =
getValidatorsAfterBlock(protocolContext, parentHeader);

final ProposalValidator proposalValidator =
new ProposalValidator(
Expand Down

0 comments on commit 9d276ae

Please sign in to comment.