From bdb9c1ad8527004e99e87ee609ecd0785b654d73 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Wed, 28 Jul 2021 17:25:57 +1000 Subject: [PATCH] Bft validator provider (#2570) Signed-off-by: Jason Frame --- .../CliqueBesuControllerBuilder.java | 14 +--- .../controller/IbftBesuControllerBuilder.java | 32 +++---- .../IbftLegacyBesuControllerBuilder.java | 18 ++-- .../controller/QbftBesuControllerBuilder.java | 26 ++---- .../clique/CliqueBlockInterface.java | 4 +- .../besu/consensus/clique/CliqueContext.java | 20 ++--- .../clique/CliqueDifficultyCalculator.java | 2 +- .../besu/consensus/clique/CliqueHelpers.java | 33 ++++---- .../consensus/clique/CliqueMiningTracker.java | 2 +- .../blockcreation/CliqueBlockCreator.java | 16 ++-- .../blockcreation/CliqueBlockScheduler.java | 18 ++-- .../blockcreation/CliqueMinerExecutor.java | 11 +-- .../blockcreation/CliqueProposerSelector.java | 16 ++-- .../CliqueExtraDataValidationRule.java | 8 +- .../clique/jsonrpc/CliqueJsonRpcMethods.java | 32 +++---- .../methods/CliqueGetSignerMetrics.java | 6 +- .../jsonrpc/methods/CliqueGetSigners.java | 10 +-- .../methods/CliqueGetSignersAtHash.java | 10 +-- .../jsonrpc/methods/CliqueProposals.java | 6 +- .../clique/jsonrpc/methods/Discard.java | 13 +-- .../clique/jsonrpc/methods/Propose.java | 15 ++-- .../clique/CliqueBlockInterfaceTest.java | 6 +- .../CliqueDifficultyCalculatorTest.java | 12 +-- .../clique/NodeCanProduceNextBlockTest.java | 60 +++++-------- .../blockcreation/CliqueBlockCreatorTest.java | 61 ++++---------- .../CliqueBlockSchedulerTest.java | 15 ++-- .../CliqueMinerExecutorTest.java | 12 +-- .../CliqueMiningCoordinatorTest.java | 13 +-- .../CliqueProposerSelectorTest.java | 12 ++- .../CliqueDifficultyValidationRuleTest.java | 12 +-- .../CliqueExtraDataValidationRuleTest.java | 10 +-- .../methods/CliqueGetSignerMetricsTest.java | 12 ++- .../methods/CliqueGetSignersAtHashTest.java | 11 +-- .../jsonrpc/methods/CliqueGetSignersTest.java | 14 ++-- .../jsonrpc/methods/CliqueProposalsTest.java | 2 +- .../clique/jsonrpc/methods/DiscardTest.java | 61 +++++++++----- .../clique/jsonrpc/methods/ProposeTest.java | 77 ++++++++++------- .../besu/consensus/common/BlockInterface.java | 1 + .../common/bft/BftBlockInterface.java | 4 +- .../besu/consensus/common/bft/BftContext.java | 20 ++--- .../besu/consensus/common/bft/Vote.java | 2 +- .../blockcreation/BftBlockCreatorFactory.java | 15 ++-- .../bft/blockcreation/ProposerSelector.java | 11 ++- .../BftCoinbaseValidationRule.java | 9 +- .../BftCommitSealsValidationRule.java | 7 +- .../BftValidatorsValidationRule.java | 6 +- .../common/bft/network/ValidatorPeers.java | 10 +-- .../bft/statemachine/BftFinalState.java | 10 +-- .../AbstractGetSignerMetricsMethod.java | 13 ++- .../jsonrpc/AbstractVoteProposerMethod.java | 31 ++++--- .../{ => validator}/ValidatorProvider.java | 11 ++- .../common/{ => validator}/ValidatorVote.java | 4 +- .../common/validator/VoteProvider.java | 34 ++++++++ .../common/{ => validator}/VoteType.java | 2 +- .../blockbased/BlockValidatorProvider.java | 84 +++++++++++++++++++ .../blockbased/BlockVoteProvider.java | 62 ++++++++++++++ .../blockbased}/ForkingVoteTallyCache.java | 7 +- .../blockbased}/VoteProposer.java | 10 +-- .../{ => validator/blockbased}/VoteTally.java | 20 ++--- .../blockbased}/VoteTallyCache.java | 12 +-- .../blockbased}/VoteTallyUpdater.java | 13 +-- .../common/bft/BftContextBuilder.java | 34 +++----- .../blockcreation/ProposerSelectorTest.java | 21 +++-- .../bft/network/ValidatorPeersTest.java | 19 ++--- .../AbstractVoteProposerMethodTest.java | 17 ++-- .../ForkingVoteTallyCacheTest.java | 4 +- .../blockbased}/VoteProposerTest.java | 4 +- .../blockbased}/VoteTallyCacheTest.java | 5 +- .../blockbased}/VoteTallyCacheTestBase.java | 3 +- .../blockbased}/VoteTallyTest.java | 4 +- .../blockbased}/VoteTallyUpdaterTest.java | 6 +- .../ibft/support/TestContextBuilder.java | 22 ++--- .../consensus/ibft/IbftExtraDataCodec.java | 2 +- .../consensus/ibft/IbftLegacyContext.java | 20 ++--- .../ibft/jsonrpc/IbftJsonRpcMethods.java | 33 ++++---- .../methods/IbftDiscardValidatorVote.java | 13 +-- .../jsonrpc/methods/IbftGetPendingVotes.java | 6 +- .../jsonrpc/methods/IbftGetSignerMetrics.java | 6 +- .../methods/IbftProposeValidatorVote.java | 18 ++-- .../validation/MessageValidatorFactory.java | 5 +- .../ibft/IbftExtraDataEncoderTest.java | 2 +- .../methods/IbftDiscardValidatorVoteTest.java | 14 +++- .../methods/IbftGetPendingVotesTest.java | 2 +- .../methods/IbftGetSignerMetricsTest.java | 12 ++- .../methods/IbftProposeValidatorVoteTest.java | 16 ++-- .../ibftlegacy/IbftLegacyBlockInterface.java | 4 +- .../IbftExtraDataValidationRule.java | 9 +- ...ockHeaderValidationRulesetFactoryTest.java | 11 +-- .../IbftLegacyBlockInterfaceTest.java | 6 +- .../ibftlegacy/IbftLegacyContextBuilder.java | 14 ++-- .../qbft/support/TestContextBuilder.java | 22 ++--- .../consensus/qbft/QbftExtraDataCodec.java | 2 +- .../qbft/jsonrpc/QbftJsonRpcMethods.java | 25 +++--- .../methods/QbftDiscardValidatorVote.java | 23 +++-- .../jsonrpc/methods/QbftGetPendingVotes.java | 6 +- .../jsonrpc/methods/QbftGetSignerMetrics.java | 6 +- .../methods/QbftProposeValidatorVote.java | 41 +++++---- .../validation/MessageValidatorFactory.java | 5 +- .../qbft/QbftExtraDataCodecTest.java | 2 +- .../methods/QbftDiscardValidatorVoteTest.java | 14 +++- .../methods/QbftGetPendingVotesTest.java | 2 +- .../methods/QbftGetSignerMetricsTest.java | 12 ++- .../methods/QbftProposeValidatorVoteTest.java | 16 ++-- 103 files changed, 866 insertions(+), 742 deletions(-) rename consensus/common/src/main/java/org/hyperledger/besu/consensus/common/{ => validator}/ValidatorProvider.java (71%) rename consensus/common/src/main/java/org/hyperledger/besu/consensus/common/{ => validator}/ValidatorVote.java (93%) create mode 100644 consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/VoteProvider.java rename consensus/common/src/main/java/org/hyperledger/besu/consensus/common/{ => validator}/VoteType.java (93%) create mode 100644 consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/BlockValidatorProvider.java create mode 100644 consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/BlockVoteProvider.java rename consensus/common/src/main/java/org/hyperledger/besu/consensus/common/{ => validator/blockbased}/ForkingVoteTallyCache.java (83%) rename consensus/common/src/main/java/org/hyperledger/besu/consensus/common/{ => validator/blockbased}/VoteProposer.java (95%) rename consensus/common/src/main/java/org/hyperledger/besu/consensus/common/{ => validator/blockbased}/VoteTally.java (89%) rename consensus/common/src/main/java/org/hyperledger/besu/consensus/common/{ => validator/blockbased}/VoteTallyCache.java (93%) rename consensus/common/src/main/java/org/hyperledger/besu/consensus/common/{ => validator/blockbased}/VoteTallyUpdater.java (83%) rename consensus/common/src/test/java/org/hyperledger/besu/consensus/common/{ => validator/blockbased}/ForkingVoteTallyCacheTest.java (95%) rename consensus/common/src/test/java/org/hyperledger/besu/consensus/common/{ => validator/blockbased}/VoteProposerTest.java (97%) rename consensus/common/src/test/java/org/hyperledger/besu/consensus/common/{ => validator/blockbased}/VoteTallyCacheTest.java (95%) rename consensus/common/src/test/java/org/hyperledger/besu/consensus/common/{ => validator/blockbased}/VoteTallyCacheTestBase.java (95%) rename consensus/common/src/test/java/org/hyperledger/besu/consensus/common/{ => validator/blockbased}/VoteTallyTest.java (98%) rename consensus/common/src/test/java/org/hyperledger/besu/consensus/common/{ => validator/blockbased}/VoteTallyUpdaterTest.java (94%) diff --git a/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java index 2795c3b23ca..340d0aae89b 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java @@ -25,9 +25,7 @@ import org.hyperledger.besu.consensus.clique.jsonrpc.CliqueJsonRpcMethods; import org.hyperledger.besu.consensus.common.BlockInterface; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTallyCache; -import org.hyperledger.besu.consensus.common.VoteTallyUpdater; +import org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider; import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods; import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; @@ -88,7 +86,7 @@ protected MiningCoordinator createMiningCoordinator( miningParameters, new CliqueBlockScheduler( clock, - protocolContext.getConsensusState(CliqueContext.class).getVoteTallyCache(), + protocolContext.getConsensusState(CliqueContext.class).getValidatorProvider(), localAddress, secondsBetweenBlocks), epochManager, @@ -133,12 +131,8 @@ protected PluginServiceFactory createAdditionalPluginServices(final Blockchain b protected CliqueContext createConsensusContext( final Blockchain blockchain, final WorldStateArchive worldStateArchive) { return new CliqueContext( - new VoteTallyCache( - blockchain, - new VoteTallyUpdater(epochManager, blockInterface), - epochManager, - blockInterface), - new VoteProposer(), + BlockValidatorProvider.nonForkingValidatorProvider( + blockchain, epochManager, blockInterface), epochManager, blockInterface); } diff --git a/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java index ad20b035995..21a28ed7e79 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java @@ -17,12 +17,7 @@ import org.hyperledger.besu.config.BftConfigOptions; import org.hyperledger.besu.config.BftFork; import org.hyperledger.besu.config.GenesisConfigOptions; -import org.hyperledger.besu.consensus.common.BftValidatorOverrides; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.ForkingVoteTallyCache; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTallyCache; -import org.hyperledger.besu.consensus.common.VoteTallyUpdater; import org.hyperledger.besu.consensus.common.bft.BftContext; import org.hyperledger.besu.consensus.common.bft.BftEventQueue; import org.hyperledger.besu.consensus.common.bft.BftExecutors; @@ -43,6 +38,8 @@ import org.hyperledger.besu.consensus.common.bft.statemachine.BftEventHandler; import org.hyperledger.besu.consensus.common.bft.statemachine.BftFinalState; import org.hyperledger.besu.consensus.common.bft.statemachine.FutureMessageBuffer; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider; import org.hyperledger.besu.consensus.ibft.IbftBlockHeaderValidationRulesetFactory; import org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec; import org.hyperledger.besu.consensus.ibft.IbftGossip; @@ -140,15 +137,15 @@ protected MiningCoordinator createMiningCoordinator( bftConfig.getMiningBeneficiary().map(Address::fromHexString).orElse(localAddress), bftExtraDataCodec().get()); - // NOTE: peers should not be used for accessing the network as it does not enforce the - // "only send once" filter applied by the UniqueMessageMulticaster. - final VoteTallyCache voteTallyCache = - protocolContext.getConsensusState(BftContext.class).getVoteTallyCache(); + final ValidatorProvider validatorProvider = + protocolContext.getConsensusState(BftContext.class).getValidatorProvider(); final ProposerSelector proposerSelector = - new ProposerSelector(blockchain, bftBlockInterface().get(), true, voteTallyCache); + new ProposerSelector(blockchain, bftBlockInterface().get(), true, validatorProvider); - peers = new ValidatorPeers(voteTallyCache, IbftSubProtocol.NAME); + // NOTE: peers should not be used for accessing the network as it does not enforce the + // "only send once" filter applied by the UniqueMessageMulticaster. + peers = new ValidatorPeers(validatorProvider, IbftSubProtocol.NAME); final UniqueMessageMulticaster uniqueMessageMulticaster = new UniqueMessageMulticaster(peers, bftConfig.getGossipedHistoryLimit()); @@ -157,7 +154,7 @@ protected MiningCoordinator createMiningCoordinator( final BftFinalState finalState = new BftFinalState( - voteTallyCache, + validatorProvider, nodeKey, Util.publicKeyToAddress(nodeKey.getPublicKey()), proposerSelector, @@ -254,17 +251,12 @@ protected BftContext createConsensusContext( final BftConfigOptions ibftConfig = configOptions.getBftConfigOptions(); final EpochManager epochManager = new EpochManager(ibftConfig.getEpochLength()); - final Map> ibftValidatorForkMap = + final Map> bftValidatorForkMap = convertIbftForks(configOptions.getTransitions().getIbftForks()); return new BftContext( - new ForkingVoteTallyCache( - blockchain, - new VoteTallyUpdater(epochManager, bftBlockInterface().get()), - epochManager, - bftBlockInterface().get(), - new BftValidatorOverrides(ibftValidatorForkMap)), - new VoteProposer(), + BlockValidatorProvider.forkingValidatorProvider( + blockchain, epochManager, bftBlockInterface().get(), bftValidatorForkMap), epochManager, bftBlockInterface().get()); } diff --git a/besu/src/main/java/org/hyperledger/besu/controller/IbftLegacyBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/IbftLegacyBesuControllerBuilder.java index 83a2d636eda..e2336f9a02c 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/IbftLegacyBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/IbftLegacyBesuControllerBuilder.java @@ -17,9 +17,8 @@ import org.hyperledger.besu.config.IbftLegacyConfigOptions; import org.hyperledger.besu.consensus.common.BlockInterface; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTallyCache; -import org.hyperledger.besu.consensus.common.VoteTallyUpdater; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider; import org.hyperledger.besu.consensus.ibft.IbftLegacyContext; import org.hyperledger.besu.consensus.ibftlegacy.IbftLegacyBlockInterface; import org.hyperledger.besu.consensus.ibftlegacy.IbftProtocolSchedule; @@ -86,16 +85,11 @@ protected IbftLegacyContext createConsensusContext( final IbftLegacyConfigOptions ibftConfig = genesisConfig.getConfigOptions(genesisConfigOverrides).getIbftLegacyConfigOptions(); final EpochManager epochManager = new EpochManager(ibftConfig.getEpochLength()); - final VoteTallyCache voteTallyCache = - new VoteTallyCache( - blockchain, - new VoteTallyUpdater(epochManager, blockInterface), - epochManager, - blockInterface); + final ValidatorProvider validatorProvider = + BlockValidatorProvider.nonForkingValidatorProvider( + blockchain, epochManager, blockInterface); - final VoteProposer voteProposer = new VoteProposer(); - - return new IbftLegacyContext(voteTallyCache, voteProposer, epochManager, blockInterface); + return new IbftLegacyContext(validatorProvider, epochManager, blockInterface); } @Override diff --git a/besu/src/main/java/org/hyperledger/besu/controller/QbftBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/QbftBesuControllerBuilder.java index 47f6726680f..18993021c46 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/QbftBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/QbftBesuControllerBuilder.java @@ -17,12 +17,7 @@ import org.hyperledger.besu.config.BftConfigOptions; import org.hyperledger.besu.config.BftFork; import org.hyperledger.besu.config.GenesisConfigOptions; -import org.hyperledger.besu.consensus.common.BftValidatorOverrides; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.ForkingVoteTallyCache; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTallyCache; -import org.hyperledger.besu.consensus.common.VoteTallyUpdater; import org.hyperledger.besu.consensus.common.bft.BftContext; import org.hyperledger.besu.consensus.common.bft.BftEventQueue; import org.hyperledger.besu.consensus.common.bft.BftExecutors; @@ -43,6 +38,8 @@ import org.hyperledger.besu.consensus.common.bft.statemachine.BftEventHandler; import org.hyperledger.besu.consensus.common.bft.statemachine.BftFinalState; import org.hyperledger.besu.consensus.common.bft.statemachine.FutureMessageBuffer; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider; import org.hyperledger.besu.consensus.qbft.QbftBlockHeaderValidationRulesetFactory; import org.hyperledger.besu.consensus.qbft.QbftExtraDataCodec; import org.hyperledger.besu.consensus.qbft.QbftGossip; @@ -143,15 +140,15 @@ protected MiningCoordinator createMiningCoordinator( bftConfig.getMiningBeneficiary().map(Address::fromHexString).orElse(localAddress), bftExtraDataCodec().get()); - final VoteTallyCache voteTallyCache = - protocolContext.getConsensusState(BftContext.class).getVoteTallyCache(); + final ValidatorProvider validatorProvider = + protocolContext.getConsensusState(BftContext.class).getValidatorProvider(); final ProposerSelector proposerSelector = - new ProposerSelector(blockchain, bftBlockInterface().get(), true, voteTallyCache); + new ProposerSelector(blockchain, bftBlockInterface().get(), true, validatorProvider); // NOTE: peers should not be used for accessing the network as it does not enforce the // "only send once" filter applied by the UniqueMessageMulticaster. - peers = new ValidatorPeers(voteTallyCache, Istanbul100SubProtocol.NAME); + peers = new ValidatorPeers(validatorProvider, Istanbul100SubProtocol.NAME); final UniqueMessageMulticaster uniqueMessageMulticaster = new UniqueMessageMulticaster(peers, bftConfig.getGossipedHistoryLimit()); @@ -160,7 +157,7 @@ protected MiningCoordinator createMiningCoordinator( final BftFinalState finalState = new BftFinalState( - voteTallyCache, + validatorProvider, nodeKey, Util.publicKeyToAddress(nodeKey.getPublicKey()), proposerSelector, @@ -261,13 +258,8 @@ protected BftContext createConsensusContext( convertBftForks(configOptions.getTransitions().getQbftForks()); return new BftContext( - new ForkingVoteTallyCache( - blockchain, - new VoteTallyUpdater(epochManager, bftBlockInterface().get()), - epochManager, - bftBlockInterface().get(), - new BftValidatorOverrides(bftValidatorForkMap)), - new VoteProposer(), + BlockValidatorProvider.forkingValidatorProvider( + blockchain, epochManager, bftBlockInterface().get(), bftValidatorForkMap), epochManager, bftBlockInterface().get()); } diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueBlockInterface.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueBlockInterface.java index eeb35d09e2e..def40abd928 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueBlockInterface.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueBlockInterface.java @@ -15,8 +15,8 @@ package org.hyperledger.besu.consensus.clique; import org.hyperledger.besu.consensus.common.BlockInterface; -import org.hyperledger.besu.consensus.common.ValidatorVote; -import org.hyperledger.besu.consensus.common.VoteType; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.BlockHeaderBuilder; diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueContext.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueContext.java index a849f0657ff..a6a58a9c5c7 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueContext.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueContext.java @@ -17,8 +17,7 @@ import org.hyperledger.besu.consensus.common.BlockInterface; import org.hyperledger.besu.consensus.common.EpochManager; import org.hyperledger.besu.consensus.common.PoaContext; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; /** * Holds the data which lives "in parallel" with the importation of blocks etc. when using the @@ -26,28 +25,21 @@ */ public class CliqueContext implements PoaContext { - private final VoteTallyCache voteTallyCache; - private final VoteProposer voteProposer; + private final ValidatorProvider validatorProvider; private final EpochManager epochManager; private final BlockInterface blockInterface; public CliqueContext( - final VoteTallyCache voteTallyCache, - final VoteProposer voteProposer, + final ValidatorProvider validatorProvider, final EpochManager epochManager, final BlockInterface blockInterface) { - this.voteTallyCache = voteTallyCache; - this.voteProposer = voteProposer; + this.validatorProvider = validatorProvider; this.epochManager = epochManager; this.blockInterface = blockInterface; } - public VoteTallyCache getVoteTallyCache() { - return voteTallyCache; - } - - public VoteProposer getVoteProposer() { - return voteProposer; + public ValidatorProvider getValidatorProvider() { + return validatorProvider; } public EpochManager getEpochManager() { diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculator.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculator.java index 0be97feb536..e8a6ada56bd 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculator.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculator.java @@ -38,7 +38,7 @@ public BigInteger nextDifficulty( final Address nextProposer = CliqueHelpers.getProposerForBlockAfter( - parent, context.getConsensusState(CliqueContext.class).getVoteTallyCache()); + parent, context.getConsensusState(CliqueContext.class).getValidatorProvider()); return nextProposer.equals(localAddress) ? IN_TURN_DIFFICULTY : OUT_OF_TURN_DIFFICULTY; } } diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueHelpers.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueHelpers.java index 26606a2b1e1..b06e134181d 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueHelpers.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueHelpers.java @@ -15,14 +15,14 @@ package org.hyperledger.besu.consensus.clique; import org.hyperledger.besu.consensus.clique.blockcreation.CliqueProposerSelector; -import org.hyperledger.besu.consensus.common.ValidatorProvider; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.core.BlockHeader; +import java.util.Collection; + public class CliqueHelpers { public static Address getProposerOfBlock(final BlockHeader header) { @@ -31,34 +31,32 @@ public static Address getProposerOfBlock(final BlockHeader header) { } public static Address getProposerForBlockAfter( - final BlockHeader parent, final VoteTallyCache voteTallyCache) { - final CliqueProposerSelector proposerSelector = new CliqueProposerSelector(voteTallyCache); + final BlockHeader parent, final ValidatorProvider validatorProvider) { + final CliqueProposerSelector proposerSelector = new CliqueProposerSelector(validatorProvider); return proposerSelector.selectProposerForNextBlock(parent); } public static boolean isSigner( final Address candidate, final ProtocolContext protocolContext, final BlockHeader parent) { - final VoteTally validatorProvider = + final Collection
validators = protocolContext .getConsensusState(CliqueContext.class) - .getVoteTallyCache() - .getVoteTallyAfterBlock(parent); - return validatorProvider.getValidators().contains(candidate); + .getValidatorProvider() + .getValidatorsAfterBlock(parent); + return validators.contains(candidate); } public static boolean addressIsAllowedToProduceNextBlock( final Address candidate, final ProtocolContext protocolContext, final BlockHeader parent) { - final VoteTally validatorProvider = - protocolContext - .getConsensusState(CliqueContext.class) - .getVoteTallyCache() - .getVoteTallyAfterBlock(parent); + final ValidatorProvider validatorProvider = + protocolContext.getConsensusState(CliqueContext.class).getValidatorProvider(); if (!isSigner(candidate, protocolContext, parent)) { return false; } - final int minimumUnsignedPastBlocks = minimumBlocksSincePreviousSigning(validatorProvider); + final int minimumUnsignedPastBlocks = + minimumBlocksSincePreviousSigning(parent, validatorProvider); final Blockchain blockchain = protocolContext.getBlockchain(); int unsignedBlockCount = 0; @@ -85,8 +83,9 @@ public static boolean addressIsAllowedToProduceNextBlock( return true; } - private static int minimumBlocksSincePreviousSigning(final ValidatorProvider validatorProvider) { - final int validatorCount = validatorProvider.getValidators().size(); + private static int minimumBlocksSincePreviousSigning( + final BlockHeader parent, final ValidatorProvider validatorProvider) { + final int validatorCount = validatorProvider.getValidatorsAfterBlock(parent).size(); // The number of contiguous blocks in which a signer may only sign 1 (as taken from clique spec) final int signerLimit = (validatorCount / 2) + 1; return signerLimit - 1; diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueMiningTracker.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueMiningTracker.java index ce54940bc54..8fc3eb57f86 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueMiningTracker.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueMiningTracker.java @@ -31,7 +31,7 @@ public CliqueMiningTracker(final Address localAddress, final ProtocolContext pro public boolean isProposerAfter(final BlockHeader header) { final Address nextProposer = CliqueHelpers.getProposerForBlockAfter( - header, protocolContext.getConsensusState(CliqueContext.class).getVoteTallyCache()); + header, protocolContext.getConsensusState(CliqueContext.class).getValidatorProvider()); return localAddress.equals(nextProposer); } diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreator.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreator.java index de6bafd7991..92fdaec28af 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreator.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreator.java @@ -14,13 +14,14 @@ */ package org.hyperledger.besu.consensus.clique.blockcreation; +import static com.google.common.base.Preconditions.checkState; + import org.hyperledger.besu.consensus.clique.CliqueBlockHashing; import org.hyperledger.besu.consensus.clique.CliqueBlockInterface; import org.hyperledger.besu.consensus.clique.CliqueContext; import org.hyperledger.besu.consensus.clique.CliqueExtraData; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.ValidatorVote; -import org.hyperledger.besu.consensus.common.VoteTally; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; import org.hyperledger.besu.crypto.NodeKey; import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.blockcreation.AbstractBlockCreator; @@ -106,11 +107,14 @@ private Optional determineCliqueVote( return Optional.empty(); } else { final CliqueContext cliqueContext = protocolContext.getConsensusState(CliqueContext.class); - final VoteTally voteTally = - cliqueContext.getVoteTallyCache().getVoteTallyAfterBlock(parentHeader); + checkState( + cliqueContext.getValidatorProvider().getVoteProvider().isPresent(), + "Clique requires a vote provider"); return cliqueContext - .getVoteProposer() - .getVote(Util.publicKeyToAddress(nodeKey.getPublicKey()), voteTally); + .getValidatorProvider() + .getVoteProvider() + .get() + .getVoteAfterBlock(parentHeader, Util.publicKeyToAddress(nodeKey.getPublicKey())); } } diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockScheduler.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockScheduler.java index c90913f67a1..dd15e611816 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockScheduler.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockScheduler.java @@ -14,13 +14,13 @@ */ package org.hyperledger.besu.consensus.clique.blockcreation; -import org.hyperledger.besu.consensus.common.ValidatorProvider; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.blockcreation.DefaultBlockScheduler; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.core.BlockHeader; import java.time.Clock; +import java.util.Collection; import java.util.Random; import com.google.common.annotations.VisibleForTesting; @@ -29,17 +29,17 @@ public class CliqueBlockScheduler extends DefaultBlockScheduler { private final int OUT_OF_TURN_DELAY_MULTIPLIER_MILLIS = 500; - private final VoteTallyCache voteTallyCache; + private final ValidatorProvider validatorProvider; private final Address localNodeAddress; private final Random r = new Random(); public CliqueBlockScheduler( final Clock clock, - final VoteTallyCache voteTallyCache, + final ValidatorProvider validatorProvider, final Address localNodeAddress, final long secondsBetweenBlocks) { super(secondsBetweenBlocks, 0L, clock); - this.voteTallyCache = voteTallyCache; + this.validatorProvider = validatorProvider; this.localNodeAddress = localNodeAddress; } @@ -56,17 +56,17 @@ public BlockCreationTimeResult getNextTimestamp(final BlockHeader parentHeader) } private int calculateTurnBasedDelay(final BlockHeader parentHeader) { - final CliqueProposerSelector proposerSelector = new CliqueProposerSelector(voteTallyCache); + final CliqueProposerSelector proposerSelector = new CliqueProposerSelector(validatorProvider); final Address nextProposer = proposerSelector.selectProposerForNextBlock(parentHeader); if (nextProposer.equals(localNodeAddress)) { return 0; } - return calculatorOutOfTurnDelay(voteTallyCache.getVoteTallyAfterBlock(parentHeader)); + return calculatorOutOfTurnDelay(validatorProvider.getValidatorsAfterBlock(parentHeader)); } - private int calculatorOutOfTurnDelay(final ValidatorProvider validators) { - final int countSigners = validators.getValidators().size(); + private int calculatorOutOfTurnDelay(final Collection
validators) { + final int countSigners = validators.size(); final double multiplier = (countSigners / 2d) + 1; final int maxDelay = (int) (multiplier * OUT_OF_TURN_DELAY_MULTIPLIER_MILLIS); return r.nextInt(maxDelay) + 1; diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutor.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutor.java index 36a3725ed6b..7ab20641b84 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutor.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutor.java @@ -18,7 +18,6 @@ import org.hyperledger.besu.consensus.clique.CliqueExtraData; import org.hyperledger.besu.consensus.common.ConsensusHelpers; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.VoteTally; import org.hyperledger.besu.crypto.NodeKey; import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.blockcreation.AbstractBlockScheduler; @@ -34,6 +33,7 @@ import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.util.Subscribers; +import java.util.Collection; import java.util.List; import java.util.Optional; import java.util.function.Function; @@ -112,12 +112,13 @@ Bytes calculateExtraData(final BlockHeader parentHeader) { ConsensusHelpers.zeroLeftPad(extraData, CliqueExtraData.EXTRA_VANITY_LENGTH); // Building ON TOP of canonical head, if the next block is epoch, include validators. if (epochManager.isEpochBlock(parentHeader.getNumber() + 1)) { - final VoteTally voteTally = + + final Collection
storedValidators = protocolContext .getConsensusState(CliqueContext.class) - .getVoteTallyCache() - .getVoteTallyAfterBlock(parentHeader); - validators.addAll(voteTally.getValidators()); + .getValidatorProvider() + .getValidatorsAfterBlock(parentHeader); + validators.addAll(storedValidators); } return CliqueExtraData.encodeUnsealed(vanityDataToInsert, validators); diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueProposerSelector.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueProposerSelector.java index 583a8d3f34b..809f0580a83 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueProposerSelector.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueProposerSelector.java @@ -16,8 +16,7 @@ import static com.google.common.base.Preconditions.checkNotNull; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.core.BlockHeader; @@ -32,11 +31,11 @@ */ public class CliqueProposerSelector { - private final VoteTallyCache voteTallyCache; + private final ValidatorProvider validatorProvider; - public CliqueProposerSelector(final VoteTallyCache voteTallyCache) { - checkNotNull(voteTallyCache); - this.voteTallyCache = voteTallyCache; + public CliqueProposerSelector(final ValidatorProvider validatorProvider) { + checkNotNull(validatorProvider); + this.validatorProvider = validatorProvider; } /** @@ -46,9 +45,8 @@ public CliqueProposerSelector(final VoteTallyCache voteTallyCache) { * @return The address of the node which is to propose a block for the provided Round. */ public Address selectProposerForNextBlock(final BlockHeader parentHeader) { - - final VoteTally parentVoteTally = voteTallyCache.getVoteTallyAfterBlock(parentHeader); - final List
validatorSet = new ArrayList<>(parentVoteTally.getValidators()); + final List
validatorSet = + new ArrayList<>(validatorProvider.getValidatorsAfterBlock(parentHeader)); final long nextBlockNumber = parentHeader.getNumber() + 1L; final int indexIntoValidators = (int) (nextBlockNumber % validatorSet.size()); diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueExtraDataValidationRule.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueExtraDataValidationRule.java index 1dde4df723e..d7716e74513 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueExtraDataValidationRule.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueExtraDataValidationRule.java @@ -17,7 +17,6 @@ import org.hyperledger.besu.consensus.clique.CliqueContext; import org.hyperledger.besu.consensus.clique.CliqueExtraData; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.VoteTally; import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.core.BlockHeader; @@ -56,13 +55,12 @@ public CliqueExtraDataValidationRule(final EpochManager epochManager) { public boolean validate( final BlockHeader header, final BlockHeader parent, final ProtocolContext protocolContext) { try { - final VoteTally validatorProvider = + final Collection
storedValidators = protocolContext .getConsensusState(CliqueContext.class) - .getVoteTallyCache() - .getVoteTallyAfterBlock(parent); + .getValidatorProvider() + .getValidatorsAfterBlock(parent); - final Collection
storedValidators = validatorProvider.getValidators(); return extraDataIsValid(storedValidators, header); } catch (final RLPException ex) { diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethods.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethods.java index ba5fa3fc0c6..ddf63811d44 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethods.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethods.java @@ -23,9 +23,8 @@ import org.hyperledger.besu.consensus.clique.jsonrpc.methods.Discard; import org.hyperledger.besu.consensus.clique.jsonrpc.methods.Propose; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTallyCache; -import org.hyperledger.besu.consensus.common.VoteTallyUpdater; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider; import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; @@ -54,28 +53,29 @@ protected Map create() { final WorldStateArchive worldStateArchive = context.getWorldStateArchive(); final BlockchainQueries blockchainQueries = new BlockchainQueries(blockchain, worldStateArchive); - final VoteProposer voteProposer = - context.getConsensusState(CliqueContext.class).getVoteProposer(); + final ValidatorProvider validatorProvider = + context.getConsensusState(CliqueContext.class).getValidatorProvider(); // Must create our own voteTallyCache as using this would pollute the main voteTallyCache - final VoteTallyCache voteTallyCache = createVoteTallyCache(context, blockchain); + final ValidatorProvider readOnlyValidatorProvider = + createValidatorProvider(context, blockchain); return mapOf( - new CliqueGetSigners(blockchainQueries, voteTallyCache), - new CliqueGetSignersAtHash(blockchainQueries, voteTallyCache), - new Propose(voteProposer), - new Discard(voteProposer), - new CliqueProposals(voteProposer), - new CliqueGetSignerMetrics(voteTallyCache, new CliqueBlockInterface(), blockchainQueries)); + new CliqueGetSigners(blockchainQueries, readOnlyValidatorProvider), + new CliqueGetSignersAtHash(blockchainQueries, readOnlyValidatorProvider), + new Propose(validatorProvider), + new Discard(validatorProvider), + new CliqueProposals(validatorProvider), + new CliqueGetSignerMetrics( + readOnlyValidatorProvider, new CliqueBlockInterface(), blockchainQueries)); } - private VoteTallyCache createVoteTallyCache( + private ValidatorProvider createValidatorProvider( final ProtocolContext context, final MutableBlockchain blockchain) { final EpochManager epochManager = context.getConsensusState(CliqueContext.class).getEpochManager(); final CliqueBlockInterface cliqueBlockInterface = new CliqueBlockInterface(); - final VoteTallyUpdater voteTallyUpdater = - new VoteTallyUpdater(epochManager, cliqueBlockInterface); - return new VoteTallyCache(blockchain, voteTallyUpdater, epochManager, cliqueBlockInterface); + return BlockValidatorProvider.nonForkingValidatorProvider( + blockchain, epochManager, cliqueBlockInterface); } } diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignerMetrics.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignerMetrics.java index 2cc51ba0027..a014d7ffabf 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignerMetrics.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignerMetrics.java @@ -15,8 +15,8 @@ package org.hyperledger.besu.consensus.clique.jsonrpc.methods; import org.hyperledger.besu.consensus.common.BlockInterface; -import org.hyperledger.besu.consensus.common.VoteTallyCache; import org.hyperledger.besu.consensus.common.jsonrpc.AbstractGetSignerMetricsMethod; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; @@ -25,10 +25,10 @@ public class CliqueGetSignerMetrics extends AbstractGetSignerMetricsMethod implements JsonRpcMethod { public CliqueGetSignerMetrics( - final VoteTallyCache voteTallyCache, + final ValidatorProvider validatorProvider, final BlockInterface blockInterface, final BlockchainQueries blockchainQueries) { - super(voteTallyCache, blockInterface, blockchainQueries); + super(validatorProvider, blockInterface, blockchainQueries); } @Override diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSigners.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSigners.java index 1fbf4d8dedd..c726bb53885 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSigners.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSigners.java @@ -14,7 +14,7 @@ */ package org.hyperledger.besu.consensus.clique.jsonrpc.methods; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; @@ -33,12 +33,12 @@ public class CliqueGetSigners implements JsonRpcMethod { private final BlockchainQueries blockchainQueries; - private final VoteTallyCache voteTallyCache; + private final ValidatorProvider validatorProvider; public CliqueGetSigners( - final BlockchainQueries blockchainQueries, final VoteTallyCache voteTallyCache) { + final BlockchainQueries blockchainQueries, final ValidatorProvider validatorProvider) { this.blockchainQueries = blockchainQueries; - this.voteTallyCache = voteTallyCache; + this.validatorProvider = validatorProvider; } @Override @@ -50,7 +50,7 @@ public String getName() { public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final Optional blockHeader = determineBlockHeader(requestContext); return blockHeader - .map(bh -> voteTallyCache.getVoteTallyAfterBlock(bh).getValidators()) + .map(validatorProvider::getValidatorsAfterBlock) .map(addresses -> addresses.stream().map(Objects::toString).collect(Collectors.toList())) .map( addresses -> new JsonRpcSuccessResponse(requestContext.getRequest().getId(), addresses)) diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHash.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHash.java index e45daa69177..66ef0f27aab 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHash.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHash.java @@ -14,7 +14,7 @@ */ package org.hyperledger.besu.consensus.clique.jsonrpc.methods; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; @@ -33,12 +33,12 @@ public class CliqueGetSignersAtHash implements JsonRpcMethod { private final BlockchainQueries blockchainQueries; - private final VoteTallyCache voteTallyCache; + private final ValidatorProvider validatorProvider; public CliqueGetSignersAtHash( - final BlockchainQueries blockchainQueries, final VoteTallyCache voteTallyCache) { + final BlockchainQueries blockchainQueries, final ValidatorProvider validatorProvider) { this.blockchainQueries = blockchainQueries; - this.voteTallyCache = voteTallyCache; + this.validatorProvider = validatorProvider; } @Override @@ -50,7 +50,7 @@ public String getName() { public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final Optional blockHeader = determineBlockHeader(requestContext); return blockHeader - .map(bh -> voteTallyCache.getVoteTallyAfterBlock(bh).getValidators()) + .map(validatorProvider::getValidatorsAfterBlock) .map(addresses -> addresses.stream().map(Objects::toString).collect(Collectors.toList())) .map( addresses -> new JsonRpcSuccessResponse(requestContext.getRequest().getId(), addresses)) diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueProposals.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueProposals.java index 8661c4dff46..9cf4dad8f70 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueProposals.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueProposals.java @@ -14,15 +14,15 @@ */ package org.hyperledger.besu.consensus.clique.jsonrpc.methods; -import org.hyperledger.besu.consensus.common.VoteProposer; import org.hyperledger.besu.consensus.common.jsonrpc.AbstractVoteProposerMethod; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; public class CliqueProposals extends AbstractVoteProposerMethod implements JsonRpcMethod { - public CliqueProposals(final VoteProposer voteProposer) { - super(voteProposer); + public CliqueProposals(final ValidatorProvider validatorProvider) { + super(validatorProvider); } @Override diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/Discard.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/Discard.java index cb5d7db63b1..01853a94a35 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/Discard.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/Discard.java @@ -14,7 +14,9 @@ */ package org.hyperledger.besu.consensus.clique.jsonrpc.methods; -import org.hyperledger.besu.consensus.common.VoteProposer; +import static com.google.common.base.Preconditions.checkState; + +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; @@ -23,10 +25,10 @@ import org.hyperledger.besu.ethereum.core.Address; public class Discard implements JsonRpcMethod { - private final VoteProposer proposer; + private final ValidatorProvider validatorProvider; - public Discard(final VoteProposer proposer) { - this.proposer = proposer; + public Discard(final ValidatorProvider validatorProvider) { + this.validatorProvider = validatorProvider; } @Override @@ -36,8 +38,9 @@ public String getName() { @Override public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { + checkState(validatorProvider.getVoteProvider().isPresent(), "Clique requires a vote provider"); final Address address = requestContext.getRequiredParameter(0, Address.class); - proposer.discard(address); + validatorProvider.getVoteProvider().get().discardVote(address); return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), true); } } diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/Propose.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/Propose.java index d8fa7ee7d89..bf5d49a91d7 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/Propose.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/Propose.java @@ -14,8 +14,10 @@ */ package org.hyperledger.besu.consensus.clique.jsonrpc.methods; +import static com.google.common.base.Preconditions.checkState; + import org.hyperledger.besu.consensus.clique.CliqueBlockInterface; -import org.hyperledger.besu.consensus.common.VoteProposer; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; @@ -26,10 +28,10 @@ import org.hyperledger.besu.ethereum.core.Address; public class Propose implements JsonRpcMethod { - private final VoteProposer proposer; + private final ValidatorProvider validatorProvider; - public Propose(final VoteProposer proposer) { - this.proposer = proposer; + public Propose(final ValidatorProvider validatorProvider) { + this.validatorProvider = validatorProvider; } @Override @@ -39,6 +41,7 @@ public String getName() { @Override public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { + checkState(validatorProvider.getVoteProvider().isPresent(), "Clique requires a vote provider"); final Address address = requestContext.getRequiredParameter(0, Address.class); final Boolean auth = requestContext.getRequiredParameter(1, Boolean.class); if (address.equals(CliqueBlockInterface.NO_VOTE_SUBJECT)) { @@ -47,9 +50,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { } if (auth) { - proposer.auth(address); + validatorProvider.getVoteProvider().get().authVote(address); } else { - proposer.drop(address); + validatorProvider.getVoteProvider().get().dropVote(address); } // Return true regardless, the vote is always recorded return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), true); diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueBlockInterfaceTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueBlockInterfaceTest.java index 3010846db4e..20f170b199c 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueBlockInterfaceTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueBlockInterfaceTest.java @@ -16,10 +16,10 @@ import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; -import static org.hyperledger.besu.consensus.common.VoteType.ADD; -import static org.hyperledger.besu.consensus.common.VoteType.DROP; +import static org.hyperledger.besu.consensus.common.validator.VoteType.ADD; +import static org.hyperledger.besu.consensus.common.validator.VoteType.DROP; -import org.hyperledger.besu.consensus.common.ValidatorVote; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; import org.hyperledger.besu.ethereum.core.Address; diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculatorTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculatorTest.java index 4d337f567f7..a00f080ebe1 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculatorTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculatorTest.java @@ -19,9 +19,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; import org.hyperledger.besu.ethereum.ProtocolContext; @@ -55,12 +53,10 @@ public void setup() { validatorList.add(localAddr); validatorList.add(AddressHelpers.calculateAddressWithRespectTo(localAddr, 1)); - final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); - final VoteProposer voteProposer = new VoteProposer(); + final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); - final CliqueContext cliqueContext = - new CliqueContext(voteTallyCache, voteProposer, null, blockInterface); + final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); cliqueProtocolContext = new ProtocolContext(null, null, cliqueContext); blockHeaderBuilder = new BlockHeaderTestFixture(); } diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/NodeCanProduceNextBlockTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/NodeCanProduceNextBlockTest.java index 452e5f26777..6231bfcfee3 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/NodeCanProduceNextBlockTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/NodeCanProduceNextBlockTest.java @@ -22,9 +22,7 @@ import static org.mockito.Mockito.when; import org.hyperledger.besu.consensus.clique.headervalidationrules.SignerRateLimitValidationRule; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; import org.hyperledger.besu.ethereum.ProtocolContext; @@ -78,11 +76,9 @@ public void networkWithOneValidatorIsAllowedToCreateConsecutiveBlocks() { blockChain = createInMemoryBlockchain(genesisBlock); - final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); - final VoteProposer voteProposer = new VoteProposer(); - final CliqueContext cliqueContext = - new CliqueContext(voteTallyCache, voteProposer, null, blockInterface); + final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); + final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); cliqueProtocolContext = new ProtocolContext(blockChain, null, cliqueContext); headerBuilder.number(1).parentHash(genesisBlock.getHash()); @@ -104,11 +100,9 @@ public void networkWithTwoValidatorsIsAllowedToProduceBlockIfNotPreviousBlockPro blockChain = createInMemoryBlockchain(genesisBlock); - final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); - final VoteProposer voteProposer = new VoteProposer(); - final CliqueContext cliqueContext = - new CliqueContext(voteTallyCache, voteProposer, null, blockInterface); + final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); + final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); cliqueProtocolContext = new ProtocolContext(blockChain, null, cliqueContext); headerBuilder.number(1).parentHash(genesisBlock.getHash()); @@ -139,11 +133,9 @@ public void networkWithTwoValidatorsIsNotAllowedToProduceBlockIfIsPreviousBlockP blockChain = createInMemoryBlockchain(genesisBlock); - final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); - final VoteProposer voteProposer = new VoteProposer(); - final CliqueContext cliqueContext = - new CliqueContext(voteTallyCache, voteProposer, null, blockInterface); + final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); + final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); cliqueProtocolContext = new ProtocolContext(blockChain, null, cliqueContext); headerBuilder.parentHash(genesisBlock.getHash()).number(1); @@ -170,11 +162,9 @@ public void withThreeValidatorsMustHaveOneBlockBetweenSignings() { blockChain = createInMemoryBlockchain(genesisBlock); - final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); - final VoteProposer voteProposer = new VoteProposer(); - final CliqueContext cliqueContext = - new CliqueContext(voteTallyCache, voteProposer, null, blockInterface); + final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); + final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); cliqueProtocolContext = new ProtocolContext(blockChain, null, cliqueContext); headerBuilder.parentHash(genesisBlock.getHash()).number(1); @@ -216,11 +206,9 @@ public void signerIsValidIfInsufficientBlocksExistInHistory() { blockChain = createInMemoryBlockchain(genesisBlock); - final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); - final VoteProposer voteProposer = new VoteProposer(); - final CliqueContext cliqueContext = - new CliqueContext(voteTallyCache, voteProposer, null, blockInterface); + final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); + final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); cliqueProtocolContext = new ProtocolContext(blockChain, null, cliqueContext); headerBuilder.parentHash(genesisBlock.getHash()).number(1); @@ -246,11 +234,9 @@ public void exceptionIsThrownIfOnAnOrphanedChain() { blockChain = createInMemoryBlockchain(genesisBlock); - final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); - final VoteProposer voteProposer = new VoteProposer(); - final CliqueContext cliqueContext = - new CliqueContext(voteTallyCache, voteProposer, null, blockInterface); + final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); + final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); cliqueProtocolContext = new ProtocolContext(blockChain, null, cliqueContext); headerBuilder.parentHash(Hash.ZERO).number(3); @@ -271,11 +257,9 @@ public void nonValidatorIsNotAllowedToCreateABlock() { blockChain = createInMemoryBlockchain(genesisBlock); - final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); - final VoteProposer voteProposer = new VoteProposer(); - final CliqueContext cliqueContext = - new CliqueContext(voteTallyCache, voteProposer, null, blockInterface); + final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); + final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); cliqueProtocolContext = new ProtocolContext(blockChain, null, cliqueContext); headerBuilder.parentHash(Hash.ZERO).number(3); diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java index 1d11739e9dd..b2368c62164 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java @@ -29,9 +29,10 @@ import org.hyperledger.besu.consensus.clique.CliqueProtocolSchedule; import org.hyperledger.besu.consensus.clique.TestHelpers; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; +import org.hyperledger.besu.consensus.common.validator.VoteProvider; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.NodeKey; import org.hyperledger.besu.crypto.NodeKeyUtils; @@ -55,6 +56,7 @@ import org.hyperledger.besu.testutil.TestClock; import java.util.List; +import java.util.Optional; import com.google.common.collect.Lists; import org.apache.tuweni.bytes.Bytes; @@ -76,8 +78,9 @@ public class CliqueBlockCreatorTest { private MutableBlockchain blockchain; private ProtocolContext protocolContext; - private VoteProposer voteProposer; private EpochManager epochManager; + private ValidatorProvider validatorProvider; + private VoteProvider voteProvider; @Before public void setup() { @@ -88,11 +91,11 @@ public void setup() { final Address otherAddress = Util.publicKeyToAddress(otherKeyPair.getPublicKey()); validatorList.add(otherAddress); - final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); - voteProposer = new VoteProposer(); - final CliqueContext cliqueContext = - new CliqueContext(voteTallyCache, voteProposer, null, blockInterface); + validatorProvider = mock(ValidatorProvider.class); + voteProvider = mock(VoteProvider.class); + when(validatorProvider.getVoteProvider()).thenReturn(Optional.of(voteProvider)); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); + final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); final Block genesis = GenesisState.fromConfig(GenesisConfigFile.mainnet(), protocolSchedule).getBlock(); @@ -150,8 +153,9 @@ public void insertsValidVoteIntoConstructedBlock() { final Bytes extraData = CliqueExtraData.createWithoutProposerSeal(Bytes.wrap(new byte[32]), validatorList); final Address a1 = Address.fromHexString("5"); - voteProposer.auth(a1); final Address coinbase = AddressHelpers.ofValue(1); + when(voteProvider.getVoteAfterBlock(any(), any())) + .thenReturn(Optional.of(new ValidatorVote(VoteType.ADD, coinbase, a1))); final CliqueBlockCreator blockCreator = new CliqueBlockCreator( @@ -179,40 +183,6 @@ public void insertsValidVoteIntoConstructedBlock() { assertThat(createdBlock.getHeader().getCoinbase()).isEqualTo(a1); } - @Test - public void insertsNoVoteWhenAuthInValidators() { - final Bytes extraData = - CliqueExtraData.createWithoutProposerSeal(Bytes.wrap(new byte[32]), validatorList); - final Address a1 = Util.publicKeyToAddress(otherKeyPair.getPublicKey()); - voteProposer.auth(a1); - final Address coinbase = AddressHelpers.ofValue(1); - - final CliqueBlockCreator blockCreator = - new CliqueBlockCreator( - coinbase, - parent -> extraData, - new PendingTransactions( - TransactionPoolConfiguration.DEFAULT_TX_RETENTION_HOURS, - 5, - 5, - TestClock.fixed(), - metricsSystem, - blockchain::getChainHeadHeader, - TransactionPoolConfiguration.DEFAULT_PRICE_BUMP), - protocolContext, - protocolSchedule, - gasLimit -> gasLimit, - proposerNodeKey, - Wei.ZERO, - 0.8, - blockchain.getChainHeadHeader(), - epochManager); - - final Block createdBlock = blockCreator.createBlock(0L); - assertThat(createdBlock.getHeader().getNonce()).isEqualTo(CliqueBlockInterface.DROP_NONCE); - assertThat(createdBlock.getHeader().getCoinbase()).isEqualTo(Address.fromHexString("0")); - } - @Test public void insertsNoVoteWhenAtEpoch() { // ensure that the next block is epoch @@ -221,8 +191,9 @@ public void insertsNoVoteWhenAtEpoch() { final Bytes extraData = CliqueExtraData.createWithoutProposerSeal(Bytes.wrap(new byte[32]), validatorList); final Address a1 = Address.fromHexString("5"); - voteProposer.auth(a1); final Address coinbase = AddressHelpers.ofValue(1); + when(validatorProvider.getVoteProvider().get().getVoteAfterBlock(any(), any())) + .thenReturn(Optional.of(new ValidatorVote(VoteType.ADD, coinbase, a1))); final CliqueBlockCreator blockCreator = new CliqueBlockCreator( diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockSchedulerTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockSchedulerTest.java index e08cf48ed9f..49a0240b545 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockSchedulerTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockSchedulerTest.java @@ -19,8 +19,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; import org.hyperledger.besu.ethereum.blockcreation.AbstractBlockScheduler.BlockCreationTimeResult; @@ -43,7 +42,7 @@ public class CliqueBlockSchedulerTest { private Address localAddr; private final List
validatorList = Lists.newArrayList(); - private VoteTallyCache voteTallyCache; + private ValidatorProvider validatorProvider; private BlockHeaderTestFixture blockHeaderBuilder; @Before @@ -53,8 +52,8 @@ public void setup() { validatorList.add(localAddr); validatorList.add(AddressHelpers.calculateAddressWithRespectTo(localAddr, 1)); - voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); + validatorProvider = mock(ValidatorProvider.class); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); blockHeaderBuilder = new BlockHeaderTestFixture(); } @@ -66,7 +65,7 @@ public void inturnValidatorWaitsExactlyBlockInterval() { final long secondsBetweenBlocks = 5L; when(clock.millis()).thenReturn(currentSecondsSinceEpoch * 1000); final CliqueBlockScheduler scheduler = - new CliqueBlockScheduler(clock, voteTallyCache, localAddr, secondsBetweenBlocks); + new CliqueBlockScheduler(clock, validatorProvider, localAddr, secondsBetweenBlocks); // There are 2 validators, therefore block 2 will put localAddr as the in-turn voter, therefore // parent block should be number 1. @@ -87,7 +86,7 @@ public void outOfturnValidatorWaitsLongerThanBlockInterval() { final long secondsBetweenBlocks = 5L; when(clock.millis()).thenReturn(currentSecondsSinceEpoch * 1000); final CliqueBlockScheduler scheduler = - new CliqueBlockScheduler(clock, voteTallyCache, localAddr, secondsBetweenBlocks); + new CliqueBlockScheduler(clock, validatorProvider, localAddr, secondsBetweenBlocks); // There are 2 validators, therefore block 3 will put localAddr as the out-turn voter, therefore // parent block should be number 2. @@ -108,7 +107,7 @@ public void inTurnValidatorCreatesBlockNowIFParentTimestampSufficientlyBehindNow final long secondsBetweenBlocks = 5L; when(clock.millis()).thenReturn(currentSecondsSinceEpoch * 1000); final CliqueBlockScheduler scheduler = - new CliqueBlockScheduler(clock, voteTallyCache, localAddr, secondsBetweenBlocks); + new CliqueBlockScheduler(clock, validatorProvider, localAddr, secondsBetweenBlocks); // There are 2 validators, therefore block 2 will put localAddr as the in-turn voter, therefore // parent block should be number 1. diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java index 85c465559ea..32b64a41dbb 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java @@ -27,9 +27,7 @@ import org.hyperledger.besu.consensus.clique.CliqueExtraData; import org.hyperledger.besu.consensus.clique.CliqueProtocolSchedule; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.crypto.NodeKey; import org.hyperledger.besu.crypto.NodeKeyUtils; import org.hyperledger.besu.ethereum.ProtocolContext; @@ -78,12 +76,10 @@ public void setup() { validatorList.add(AddressHelpers.calculateAddressWithRespectTo(localAddress, 2)); validatorList.add(AddressHelpers.calculateAddressWithRespectTo(localAddress, 3)); - final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); - final VoteProposer voteProposer = new VoteProposer(); + final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); - final CliqueContext cliqueContext = - new CliqueContext(voteTallyCache, voteProposer, null, blockInterface); + final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); cliqueProtocolContext = new ProtocolContext(null, null, cliqueContext); blockHeaderBuilder = new BlockHeaderTestFixture(); } diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMiningCoordinatorTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMiningCoordinatorTest.java index a7a8556eef2..71b77fdc890 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMiningCoordinatorTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMiningCoordinatorTest.java @@ -17,7 +17,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.times; @@ -28,8 +27,7 @@ import org.hyperledger.besu.consensus.clique.CliqueContext; import org.hyperledger.besu.consensus.clique.CliqueMiningTracker; import org.hyperledger.besu.consensus.clique.TestHelpers; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.SignatureAlgorithm; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; @@ -80,7 +78,7 @@ public class CliqueMiningCoordinatorTest { @Mock private CliqueMinerExecutor minerExecutor; @Mock private CliqueBlockMiner blockMiner; @Mock private SyncState syncState; - @Mock private VoteTallyCache voteTallyCache; + @Mock private ValidatorProvider validatorProvider; @Before public void setup() { @@ -89,11 +87,8 @@ public void setup() { Block genesisBlock = createEmptyBlock(0, Hash.ZERO, proposerKeys); // not normally signed but ok blockChain = createInMemoryBlockchain(genesisBlock); - final VoteTally voteTally = mock(VoteTally.class); - when(voteTally.getValidators()).thenReturn(validators); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(voteTally); - final CliqueContext cliqueContext = - new CliqueContext(voteTallyCache, null, null, blockInterface); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validators); + final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); when(protocolContext.getConsensusState(CliqueContext.class)).thenReturn(cliqueContext); when(protocolContext.getBlockchain()).thenReturn(blockChain); diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueProposerSelectorTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueProposerSelectorTest.java index f9a1fb05d6d..88ed68cdc44 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueProposerSelectorTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueProposerSelectorTest.java @@ -19,8 +19,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.core.AddressHelpers; import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; @@ -39,13 +38,12 @@ public class CliqueProposerSelectorTest { AddressHelpers.ofValue(2), AddressHelpers.ofValue(3), AddressHelpers.ofValue(4)); - private final VoteTally voteTally = new VoteTally(validatorList); - private VoteTallyCache voteTallyCache; + private ValidatorProvider validatorProvider; @Before public void setup() { - voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(voteTally); + validatorProvider = mock(ValidatorProvider.class); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); } @Test @@ -54,7 +52,7 @@ public void proposerForABlockIsBasedOnModBlockNumber() { for (int prevBlockNumber = 0; prevBlockNumber < 10; prevBlockNumber++) { headerBuilderFixture.number(prevBlockNumber); - final CliqueProposerSelector selector = new CliqueProposerSelector(voteTallyCache); + final CliqueProposerSelector selector = new CliqueProposerSelector(validatorProvider); final Address nextProposer = selector.selectProposerForNextBlock(headerBuilderFixture.buildHeader()); assertThat(nextProposer) diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java index b0dbf807076..0a066e6ab25 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java @@ -22,9 +22,7 @@ import org.hyperledger.besu.consensus.clique.CliqueBlockInterface; import org.hyperledger.besu.consensus.clique.CliqueContext; import org.hyperledger.besu.consensus.clique.TestHelpers; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; import org.hyperledger.besu.ethereum.ProtocolContext; @@ -55,12 +53,10 @@ public void setup() { validatorList.add(localAddress); validatorList.add(AddressHelpers.calculateAddressWithRespectTo(localAddress, 1)); - final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); - final VoteProposer voteProposer = new VoteProposer(); + final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); - final CliqueContext cliqueContext = - new CliqueContext(voteTallyCache, voteProposer, null, blockInterface); + final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); cliqueProtocolContext = new ProtocolContext(null, null, cliqueContext); blockHeaderBuilder = new BlockHeaderTestFixture(); } diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueExtraDataValidationRuleTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueExtraDataValidationRuleTest.java index f1630d519c1..5b56e14c09c 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueExtraDataValidationRuleTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueExtraDataValidationRuleTest.java @@ -24,8 +24,7 @@ import org.hyperledger.besu.consensus.clique.CliqueExtraData; import org.hyperledger.besu.consensus.clique.TestHelpers; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; import org.hyperledger.besu.ethereum.ProtocolContext; @@ -58,11 +57,10 @@ public void setup() { validatorList.add(localAddr); validatorList.add(AddressHelpers.calculateAddressWithRespectTo(localAddr, 1)); - final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); + final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); - final CliqueContext cliqueContext = - new CliqueContext(voteTallyCache, null, null, blockInterface); + final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); cliqueProtocolContext = new ProtocolContext(null, null, cliqueContext); } diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignerMetricsTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignerMetricsTest.java index 8b68508131c..dda2403362f 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignerMetricsTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignerMetricsTest.java @@ -22,8 +22,7 @@ import static org.mockito.Mockito.when; import org.hyperledger.besu.consensus.common.BlockInterface; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; 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.exception.InvalidJsonRpcParameters; @@ -56,7 +55,7 @@ public class CliqueGetSignerMetricsTest { private final String JSON_RPC_VERSION = "2.0"; private CliqueGetSignerMetrics method; - private VoteTallyCache voteTallyCache; + private ValidatorProvider validatorProvider; private BlockchainQueries blockchainQueries; private BlockInterface blockInterface; @@ -64,10 +63,10 @@ public class CliqueGetSignerMetricsTest { @Before public void setup() { - voteTallyCache = mock(VoteTallyCache.class); + validatorProvider = mock(ValidatorProvider.class); blockchainQueries = mock(BlockchainQueries.class); blockInterface = mock(BlockInterface.class); - method = new CliqueGetSignerMetrics(voteTallyCache, blockInterface, blockchainQueries); + method = new CliqueGetSignerMetrics(validatorProvider, blockInterface, blockchainQueries); } @Test @@ -261,8 +260,7 @@ private SignerMetricResult generateBlock(final long number) { when(blockchainQueries.getBlockHeaderByNumber(number)).thenReturn(Optional.of(header)); when(blockInterface.getProposerOfBlock(header)).thenReturn(proposerAddressBlock); - when(voteTallyCache.getVoteTallyAfterBlock(header)) - .thenReturn(new VoteTally(Arrays.asList(VALIDATORS))); + when(validatorProvider.getValidatorsAfterBlock(header)).thenReturn((Arrays.asList(VALIDATORS))); final SignerMetricResult signerMetricResult = new SignerMetricResult(proposerAddressBlock); signerMetricResult.incrementeNbBlock(); diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHashTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHashTest.java index 76b9fbd017e..64837ef8b66 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHashTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHashTest.java @@ -21,8 +21,7 @@ import static org.mockito.Mockito.when; import org.hyperledger.besu.consensus.clique.CliqueBlockHeaderFunctions; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; 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.exception.InvalidJsonRpcParameters; @@ -58,16 +57,15 @@ public class CliqueGetSignersAtHashTest { private List validatorsAsStrings; @Mock private BlockchainQueries blockchainQueries; - @Mock private VoteTallyCache voteTallyCache; @Mock private BlockWithMetadata blockWithMetadata; - @Mock private VoteTally voteTally; + @Mock private ValidatorProvider validatorProvider; public static final String BLOCK_HASH = "0xe36a3edf0d8664002a72ef7c5f8e271485e7ce5c66455a07cb679d855818415f"; @Before public void setup() { - method = new CliqueGetSignersAtHash(blockchainQueries, voteTallyCache); + method = new CliqueGetSignersAtHash(blockchainQueries, validatorProvider); final byte[] genesisBlockExtraData = Hex.decode( @@ -116,8 +114,7 @@ public void returnsValidatorsForBlockHash() { when(blockchainQueries.blockByHash(Hash.fromHexString(BLOCK_HASH))) .thenReturn(Optional.of(blockWithMetadata)); when(blockWithMetadata.getHeader()).thenReturn(blockHeader); - when(voteTallyCache.getVoteTallyAfterBlock(blockHeader)).thenReturn(voteTally); - when(voteTally.getValidators()).thenReturn(validators); + when(validatorProvider.getValidatorsAfterBlock(blockHeader)).thenReturn(validators); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); assertThat(response.getResult()).isEqualTo(validatorsAsStrings); diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersTest.java index 5780408882b..b4e8a70bb98 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersTest.java @@ -20,8 +20,7 @@ import static org.hyperledger.besu.ethereum.core.Address.fromHexString; import static org.mockito.Mockito.when; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; 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.response.JsonRpcError; @@ -53,13 +52,12 @@ public class CliqueGetSignersTest { private List validatorAsStrings; @Mock private BlockchainQueries blockchainQueries; - @Mock private VoteTallyCache voteTallyCache; + @Mock private ValidatorProvider validatorProvider; @Mock private BlockWithMetadata blockWithMetadata; - @Mock private VoteTally voteTally; @Before public void setup() { - method = new CliqueGetSigners(blockchainQueries, voteTallyCache); + method = new CliqueGetSigners(blockchainQueries, validatorProvider); final String genesisBlockExtraData = "52657370656374206d7920617574686f7269746168207e452e436172746d616e42eb768f2244c8811c63729a21a3569731535f067ffc57839b00206d1ad20c69a1981b489f772031b279182d99e65703f0076e4812653aab85fca0f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; @@ -89,8 +87,7 @@ public void returnsValidatorsWhenNoParam() { when(blockchainQueries.headBlockNumber()).thenReturn(3065995L); when(blockchainQueries.blockByNumber(3065995L)).thenReturn(Optional.of(blockWithMetadata)); when(blockWithMetadata.getHeader()).thenReturn(blockHeader); - when(voteTallyCache.getVoteTallyAfterBlock(blockHeader)).thenReturn(voteTally); - when(voteTally.getValidators()).thenReturn(validators); + when(validatorProvider.getValidatorsAfterBlock(blockHeader)).thenReturn(validators); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); assertThat(response.getResult()).isEqualTo(validatorAsStrings); @@ -105,8 +102,7 @@ public void returnsValidatorsForBlockNumber() { when(blockchainQueries.blockByNumber(3065995L)).thenReturn(Optional.of(blockWithMetadata)); when(blockWithMetadata.getHeader()).thenReturn(blockHeader); - when(voteTallyCache.getVoteTallyAfterBlock(blockHeader)).thenReturn(voteTally); - when(voteTally.getValidators()).thenReturn(validators); + when(validatorProvider.getValidatorsAfterBlock(blockHeader)).thenReturn(validators); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); assertThat(response.getResult()).isEqualTo(validatorAsStrings); diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueProposalsTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueProposalsTest.java index 70d65d67169..123f1b8cbb7 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueProposalsTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueProposalsTest.java @@ -38,7 +38,7 @@ protected String getMethodName() { @Before public void setup() { - method = new CliqueProposals(getVoteProposer()); + method = new CliqueProposals(getValidatorProvider()); } @Test diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/DiscardTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/DiscardTest.java index c6912034bcd..8f5e32dc4f1 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/DiscardTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/DiscardTest.java @@ -16,50 +16,67 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteType; +import org.hyperledger.besu.consensus.common.BlockInterface; +import org.hyperledger.besu.consensus.common.EpochManager; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.VoteType; +import org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider; 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.exception.InvalidJsonRpcParameters; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; +import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Address; -import java.util.Optional; - +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +@RunWith(MockitoJUnitRunner.class) public class DiscardTest { private final String JSON_RPC_VERSION = "2.0"; private final String METHOD = "clique_discard"; + private ValidatorProvider validatorProvider; + + @Before + public void setup() { + final Blockchain blockchain = mock(Blockchain.class); + final EpochManager epochManager = mock(EpochManager.class); + final BlockInterface blockInterface = mock(BlockInterface.class); + validatorProvider = + BlockValidatorProvider.nonForkingValidatorProvider( + blockchain, epochManager, blockInterface); + } + @Test public void discardEmpty() { - final VoteProposer proposer = new VoteProposer(); - final Discard discard = new Discard(proposer); + final Discard discard = new Discard(validatorProvider); final Address a0 = Address.fromHexString("0"); final JsonRpcResponse response = discard.response(requestWithParams(a0)); - assertThat(proposer.get(a0)).isEqualTo(Optional.empty()); assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS); final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response; assertThat(successResponse.getResult()).isEqualTo(true); + assertThat(validatorProvider.getVoteProvider().get().getProposals().get(a0)).isNull(); } @Test public void discardAuth() { - final VoteProposer proposer = new VoteProposer(); - final Discard discard = new Discard(proposer); + final Discard discard = new Discard(validatorProvider); final Address a0 = Address.fromHexString("0"); - proposer.auth(a0); + validatorProvider.getVoteProvider().get().authVote(a0); final JsonRpcResponse response = discard.response(requestWithParams(a0)); - assertThat(proposer.get(a0)).isEqualTo(Optional.empty()); + assertThat(validatorProvider.getVoteProvider().get().getProposals().get(a0)).isNull(); assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS); final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response; assertThat(successResponse.getResult()).isEqualTo(true); @@ -67,15 +84,14 @@ public void discardAuth() { @Test public void discardDrop() { - final VoteProposer proposer = new VoteProposer(); - final Discard discard = new Discard(proposer); + final Discard discard = new Discard(validatorProvider); final Address a0 = Address.fromHexString("0"); - proposer.drop(a0); + validatorProvider.getVoteProvider().get().dropVote(a0); final JsonRpcResponse response = discard.response(requestWithParams(a0)); - assertThat(proposer.get(a0)).isEqualTo(Optional.empty()); + assertThat(validatorProvider.getVoteProvider().get().getProposals().get(a0)).isNull(); assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS); final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response; assertThat(successResponse.getResult()).isEqualTo(true); @@ -83,18 +99,18 @@ public void discardDrop() { @Test public void discardIsolation() { - final VoteProposer proposer = new VoteProposer(); - final Discard discard = new Discard(proposer); + final Discard discard = new Discard(validatorProvider); final Address a0 = Address.fromHexString("0"); final Address a1 = Address.fromHexString("1"); - proposer.auth(a0); - proposer.auth(a1); + validatorProvider.getVoteProvider().get().authVote(a0); + validatorProvider.getVoteProvider().get().authVote(a1); final JsonRpcResponse response = discard.response(requestWithParams(a0)); - assertThat(proposer.get(a0)).isEqualTo(Optional.empty()); - assertThat(proposer.get(a1)).isEqualTo(Optional.of(VoteType.ADD)); + assertThat(validatorProvider.getVoteProvider().get().getProposals().get(a0)).isNull(); + assertThat(validatorProvider.getVoteProvider().get().getProposals().get(a1)) + .isEqualTo(VoteType.ADD); assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS); final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response; assertThat(successResponse.getResult()).isEqualTo(true); @@ -102,8 +118,7 @@ public void discardIsolation() { @Test public void discardWithoutAddress() { - final VoteProposer proposer = new VoteProposer(); - final Discard discard = new Discard(proposer); + final Discard discard = new Discard(validatorProvider); assertThatThrownBy(() -> discard.response(requestWithParams())) .hasMessage("Missing required json rpc parameter at index 0") diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/ProposeTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/ProposeTest.java index 09169c9798b..5515e69449b 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/ProposeTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/ProposeTest.java @@ -15,9 +15,13 @@ package org.hyperledger.besu.consensus.clique.jsonrpc.methods; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteType; +import org.hyperledger.besu.consensus.common.BlockInterface; +import org.hyperledger.besu.consensus.common.EpochManager; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.VoteType; +import org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider; 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.response.JsonRpcError; @@ -25,25 +29,36 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; +import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Address; -import java.util.Optional; - +import org.junit.Before; import org.junit.Test; public class ProposeTest { private final String JSON_RPC_VERSION = "2.0"; private final String METHOD = "clique_propose"; + private ValidatorProvider validatorProvider; + + @Before + public void setup() { + final Blockchain blockchain = mock(Blockchain.class); + final EpochManager epochManager = mock(EpochManager.class); + final BlockInterface blockInterface = mock(BlockInterface.class); + validatorProvider = + BlockValidatorProvider.nonForkingValidatorProvider( + blockchain, epochManager, blockInterface); + } @Test public void testAuth() { - final VoteProposer proposer = new VoteProposer(); - final Propose propose = new Propose(proposer); + final Propose propose = new Propose(validatorProvider); final Address a1 = Address.fromHexString("1"); final JsonRpcResponse response = propose.response(requestWithParams(a1, true)); - assertThat(proposer.get(a1)).isEqualTo(Optional.of(VoteType.ADD)); + assertThat(validatorProvider.getVoteProvider().get().getProposals().get(a1)) + .isEqualTo(VoteType.ADD); assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS); final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response; assertThat(successResponse.getResult()).isEqualTo(true); @@ -51,13 +66,12 @@ public void testAuth() { @Test public void testAuthWithAddressZeroResultsInError() { - final VoteProposer proposer = new VoteProposer(); - final Propose propose = new Propose(proposer); + final Propose propose = new Propose(validatorProvider); final Address a0 = Address.fromHexString("0"); final JsonRpcResponse response = propose.response(requestWithParams(a0, true)); - assertThat(proposer.get(a0)).isEqualTo(Optional.empty()); + assertThat(validatorProvider.getVoteProvider().get().getProposals().get(a0)).isNull(); assertThat(response.getType()).isEqualTo(JsonRpcResponseType.ERROR); final JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) response; assertThat(errorResponse.getError()).isEqualTo(JsonRpcError.INVALID_REQUEST); @@ -65,13 +79,13 @@ public void testAuthWithAddressZeroResultsInError() { @Test public void testDrop() { - final VoteProposer proposer = new VoteProposer(); - final Propose propose = new Propose(proposer); + final Propose propose = new Propose(validatorProvider); final Address a1 = Address.fromHexString("1"); final JsonRpcResponse response = propose.response(requestWithParams(a1, false)); - assertThat(proposer.get(a1)).isEqualTo(Optional.of(VoteType.DROP)); + assertThat(validatorProvider.getVoteProvider().get().getProposals().get(a1)) + .isEqualTo(VoteType.DROP); assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS); final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response; assertThat(successResponse.getResult()).isEqualTo(true); @@ -79,13 +93,12 @@ public void testDrop() { @Test public void testDropWithAddressZeroResultsInError() { - final VoteProposer proposer = new VoteProposer(); - final Propose propose = new Propose(proposer); + final Propose propose = new Propose(validatorProvider); final Address a0 = Address.fromHexString("0"); final JsonRpcResponse response = propose.response(requestWithParams(a0, false)); - assertThat(proposer.get(a0)).isEqualTo(Optional.empty()); + assertThat(validatorProvider.getVoteProvider().get().getProposals().get(a0)).isNull(); assertThat(response.getType()).isEqualTo(JsonRpcResponseType.ERROR); final JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) response; assertThat(errorResponse.getError()).isEqualTo(JsonRpcError.INVALID_REQUEST); @@ -93,14 +106,14 @@ public void testDropWithAddressZeroResultsInError() { @Test public void testRepeatAuth() { - final VoteProposer proposer = new VoteProposer(); - final Propose propose = new Propose(proposer); + final Propose propose = new Propose(validatorProvider); final Address a1 = Address.fromHexString("1"); - proposer.auth(a1); + validatorProvider.getVoteProvider().get().authVote(a1); final JsonRpcResponse response = propose.response(requestWithParams(a1, true)); - assertThat(proposer.get(a1)).isEqualTo(Optional.of(VoteType.ADD)); + assertThat(validatorProvider.getVoteProvider().get().getProposals().get(a1)) + .isEqualTo(VoteType.ADD); assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS); final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response; assertThat(successResponse.getResult()).isEqualTo(true); @@ -108,14 +121,14 @@ public void testRepeatAuth() { @Test public void testRepeatDrop() { - final VoteProposer proposer = new VoteProposer(); - final Propose propose = new Propose(proposer); + final Propose propose = new Propose(validatorProvider); final Address a1 = Address.fromHexString("1"); - proposer.drop(a1); + validatorProvider.getVoteProvider().get().dropVote(a1); final JsonRpcResponse response = propose.response(requestWithParams(a1, false)); - assertThat(proposer.get(a1)).isEqualTo(Optional.of(VoteType.DROP)); + assertThat(validatorProvider.getVoteProvider().get().getProposals().get(a1)) + .isEqualTo(VoteType.DROP); assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS); final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response; assertThat(successResponse.getResult()).isEqualTo(true); @@ -123,14 +136,14 @@ public void testRepeatDrop() { @Test public void testChangeToAuth() { - final VoteProposer proposer = new VoteProposer(); - final Propose propose = new Propose(proposer); + final Propose propose = new Propose(validatorProvider); final Address a1 = Address.fromHexString("1"); - proposer.drop(a1); + validatorProvider.getVoteProvider().get().dropVote(a1); final JsonRpcResponse response = propose.response(requestWithParams(a1, true)); - assertThat(proposer.get(a1)).isEqualTo(Optional.of(VoteType.ADD)); + assertThat(validatorProvider.getVoteProvider().get().getProposals().get(a1)) + .isEqualTo(VoteType.ADD); assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS); final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response; assertThat(successResponse.getResult()).isEqualTo(true); @@ -138,14 +151,14 @@ public void testChangeToAuth() { @Test public void testChangeToDrop() { - final VoteProposer proposer = new VoteProposer(); - final Propose propose = new Propose(proposer); + final Propose propose = new Propose(validatorProvider); final Address a0 = Address.fromHexString("1"); - proposer.auth(a0); + validatorProvider.getVoteProvider().get().authVote(a0); final JsonRpcResponse response = propose.response(requestWithParams(a0, false)); - assertThat(proposer.get(a0)).isEqualTo(Optional.of(VoteType.DROP)); + assertThat(validatorProvider.getVoteProvider().get().getProposals().get(a0)) + .isEqualTo(VoteType.DROP); assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS); final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response; assertThat(successResponse.getResult()).isEqualTo(true); diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/BlockInterface.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/BlockInterface.java index ee9af867eff..b24f3059e95 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/BlockInterface.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/BlockInterface.java @@ -14,6 +14,7 @@ */ package org.hyperledger.besu.consensus.common; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.core.BlockHeader; diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BftBlockInterface.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BftBlockInterface.java index 4d2b21a4374..6435cf851c7 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BftBlockInterface.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BftBlockInterface.java @@ -15,8 +15,8 @@ package org.hyperledger.besu.consensus.common.bft; import org.hyperledger.besu.consensus.common.BlockInterface; -import org.hyperledger.besu.consensus.common.ValidatorVote; -import org.hyperledger.besu.consensus.common.VoteType; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.core.Block; import org.hyperledger.besu.ethereum.core.BlockHeader; diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BftContext.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BftContext.java index 164550acf77..9e45d975362 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BftContext.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BftContext.java @@ -16,34 +16,26 @@ import org.hyperledger.besu.consensus.common.EpochManager; import org.hyperledger.besu.consensus.common.PoaContext; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; /** Holds the BFT specific mutable state. */ public class BftContext implements PoaContext { - private final VoteTallyCache voteTallyCache; - private final VoteProposer voteProposer; + private final ValidatorProvider validatorProvider; private final EpochManager epochManager; private final BftBlockInterface blockInterface; public BftContext( - final VoteTallyCache voteTallyCache, - final VoteProposer voteProposer, + final ValidatorProvider validatorProvider, final EpochManager epochManager, final BftBlockInterface blockInterface) { - this.voteTallyCache = voteTallyCache; - this.voteProposer = voteProposer; + this.validatorProvider = validatorProvider; this.epochManager = epochManager; this.blockInterface = blockInterface; } - public VoteTallyCache getVoteTallyCache() { - return voteTallyCache; - } - - public VoteProposer getVoteProposer() { - return voteProposer; + public ValidatorProvider getValidatorProvider() { + return validatorProvider; } public EpochManager getEpochManager() { diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/Vote.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/Vote.java index 1774f060942..a0c2f28d7a5 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/Vote.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/Vote.java @@ -14,7 +14,7 @@ */ package org.hyperledger.besu.consensus.common.bft; -import org.hyperledger.besu.consensus.common.VoteType; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.ethereum.core.Address; import java.util.Objects; diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/blockcreation/BftBlockCreatorFactory.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/blockcreation/BftBlockCreatorFactory.java index 63bda0c6660..f26f5770b94 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/blockcreation/BftBlockCreatorFactory.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/blockcreation/BftBlockCreatorFactory.java @@ -14,13 +14,15 @@ */ package org.hyperledger.besu.consensus.common.bft.blockcreation; +import static com.google.common.base.Preconditions.checkState; + import org.hyperledger.besu.consensus.common.ConsensusHelpers; -import org.hyperledger.besu.consensus.common.ValidatorVote; -import org.hyperledger.besu.consensus.common.VoteTally; import org.hyperledger.besu.consensus.common.bft.BftContext; import org.hyperledger.besu.consensus.common.bft.BftExtraData; import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec; import org.hyperledger.besu.consensus.common.bft.Vote; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.blockcreation.GasLimitCalculator; import org.hyperledger.besu.ethereum.core.Address; @@ -101,12 +103,13 @@ public Wei getMinTransactionGasPrice() { public Bytes createExtraData(final int round, final BlockHeader parentHeader) { final BftContext bftContext = protocolContext.getConsensusState(BftContext.class); - final VoteTally voteTally = bftContext.getVoteTallyCache().getVoteTallyAfterBlock(parentHeader); - + final ValidatorProvider validatorProvider = bftContext.getValidatorProvider(); + checkState(validatorProvider.getVoteProvider().isPresent(), "Bft requires a vote provider"); final Optional proposal = - bftContext.getVoteProposer().getVote(localAddress, voteTally); + validatorProvider.getVoteProvider().get().getVoteAfterBlock(parentHeader, localAddress); - final List
validators = new ArrayList<>(voteTally.getValidators()); + final List
validators = + new ArrayList<>(validatorProvider.getValidatorsAfterBlock(parentHeader)); final BftExtraData extraData = new BftExtraData( diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/blockcreation/ProposerSelector.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/blockcreation/ProposerSelector.java index 72cdb73cfbb..671b6431450 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/blockcreation/ProposerSelector.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/blockcreation/ProposerSelector.java @@ -17,9 +17,8 @@ import static com.google.common.base.Preconditions.checkArgument; import org.hyperledger.besu.consensus.common.BlockInterface; -import org.hyperledger.besu.consensus.common.ValidatorProvider; -import org.hyperledger.besu.consensus.common.VoteTallyCache; import org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.core.BlockHeader; @@ -55,7 +54,7 @@ public class ProposerSelector { */ private final Boolean changeEachBlock; - private final VoteTallyCache voteTallyCache; + private final ValidatorProvider validatorProvider; private final BlockInterface blockInterface; @@ -63,11 +62,11 @@ public ProposerSelector( final Blockchain blockchain, final BlockInterface blockInterface, final boolean changeEachBlock, - final VoteTallyCache voteTallyCache) { + final ValidatorProvider validatorProvider) { this.blockchain = blockchain; this.blockInterface = blockInterface; this.changeEachBlock = changeEachBlock; - this.voteTallyCache = voteTallyCache; + this.validatorProvider = validatorProvider; } /** @@ -90,7 +89,7 @@ public Address selectProposerForRound(final ConsensusRoundIdentifier roundIdenti final BlockHeader blockHeader = maybeParentHeader.get(); final Address prevBlockProposer = blockInterface.getProposerOfBlock(blockHeader); final Collection
validatorsForRound = - voteTallyCache.getVoteTallyAfterBlock(blockHeader).getValidators(); + validatorProvider.getValidatorsAfterBlock(blockHeader); if (!validatorsForRound.contains(prevBlockProposer)) { return handleMissingProposer(prevBlockProposer, validatorsForRound, roundIdentifier); diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftCoinbaseValidationRule.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftCoinbaseValidationRule.java index 685ed7c16ec..e667313c8a0 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftCoinbaseValidationRule.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftCoinbaseValidationRule.java @@ -14,7 +14,6 @@ */ package org.hyperledger.besu.consensus.common.bft.headervalidationrules; -import org.hyperledger.besu.consensus.common.ValidatorProvider; import org.hyperledger.besu.consensus.common.bft.BftContext; import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.core.Address; @@ -38,15 +37,13 @@ public class BftCoinbaseValidationRule implements AttachedBlockHeaderValidationR public boolean validate( final BlockHeader header, final BlockHeader parent, final ProtocolContext context) { - final ValidatorProvider validatorProvider = + final Collection
storedValidators = context .getConsensusState(BftContext.class) - .getVoteTallyCache() - .getVoteTallyAfterBlock(parent); + .getValidatorProvider() + .getValidatorsAfterBlock(parent); final Address proposer = header.getCoinbase(); - final Collection
storedValidators = validatorProvider.getValidators(); - if (!storedValidators.contains(proposer)) { LOGGER.info( "Invalid block header: Block proposer is not a member of the validators. proposer={}, validators={}", diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftCommitSealsValidationRule.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftCommitSealsValidationRule.java index a139d25faee..4c585a2ce93 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftCommitSealsValidationRule.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftCommitSealsValidationRule.java @@ -16,7 +16,6 @@ import static org.hyperledger.besu.consensus.common.bft.BftHelpers.calculateRequiredValidatorQuorum; -import org.hyperledger.besu.consensus.common.ValidatorProvider; import org.hyperledger.besu.consensus.common.bft.BftContext; import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.core.Address; @@ -45,8 +44,8 @@ public class BftCommitSealsValidationRule implements AttachedBlockHeaderValidati public boolean validate( final BlockHeader header, final BlockHeader parent, final ProtocolContext protocolContext) { final BftContext bftContext = protocolContext.getConsensusState(BftContext.class); - final ValidatorProvider validatorProvider = - bftContext.getVoteTallyCache().getVoteTallyAfterBlock(parent); + final Collection
storedValidators = + bftContext.getValidatorProvider().getValidatorsAfterBlock(parent); final List
committers = bftContext.getBlockInterface().getCommitters(header); final List
committersWithoutDuplicates = new ArrayList<>(new HashSet<>(committers)); @@ -56,7 +55,7 @@ public boolean validate( return false; } - return validateCommitters(committersWithoutDuplicates, validatorProvider.getValidators()); + return validateCommitters(committersWithoutDuplicates, storedValidators); } private boolean validateCommitters( diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftValidatorsValidationRule.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftValidatorsValidationRule.java index db668d93032..8c678ddfe42 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftValidatorsValidationRule.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/headervalidationrules/BftValidatorsValidationRule.java @@ -14,7 +14,6 @@ */ package org.hyperledger.besu.consensus.common.bft.headervalidationrules; -import org.hyperledger.besu.consensus.common.ValidatorProvider; import org.hyperledger.besu.consensus.common.bft.BftContext; import org.hyperledger.besu.consensus.common.bft.BftExtraData; import org.hyperledger.besu.ethereum.ProtocolContext; @@ -44,8 +43,6 @@ public boolean validate( final BlockHeader header, final BlockHeader parent, final ProtocolContext context) { try { final BftContext bftContext = context.getConsensusState(BftContext.class); - final ValidatorProvider validatorProvider = - bftContext.getVoteTallyCache().getVoteTallyAfterBlock(parent); final BftExtraData bftExtraData = bftContext.getBlockInterface().getExtraData(header); final NavigableSet
sortedReportedValidators = @@ -59,7 +56,8 @@ public boolean validate( return false; } - final Collection
storedValidators = validatorProvider.getValidators(); + final Collection
storedValidators = + bftContext.getValidatorProvider().getValidatorsAfterBlock(parent); if (!Iterables.elementsEqual(bftExtraData.getValidators(), storedValidators)) { LOGGER.info( "Invalid block header: Incorrect validators. Expected {} but got {}.", diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/network/ValidatorPeers.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/network/ValidatorPeers.java index d9b4968640e..095baa0ac65 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/network/ValidatorPeers.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/network/ValidatorPeers.java @@ -14,7 +14,7 @@ */ package org.hyperledger.besu.consensus.common.bft.network; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection; import org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection.PeerNotConnected; @@ -42,11 +42,11 @@ public class ValidatorPeers implements ValidatorMulticaster, PeerConnectionTrack // It's possible for multiple connections between peers to exist for brief periods, so map each // address to a set of connections private final Map> connectionsByAddress = new ConcurrentHashMap<>(); - private final VoteTallyCache voteTallyCache; + private final ValidatorProvider validatorProvider; private final String protocolName; - public ValidatorPeers(final VoteTallyCache voteTallyCache, final String protocolName) { - this.voteTallyCache = voteTallyCache; + public ValidatorPeers(final ValidatorProvider validatorProvider, final String protocolName) { + this.validatorProvider = validatorProvider; this.protocolName = protocolName; } @@ -105,6 +105,6 @@ private void sendMessageToSpecificAddresses( } private Collection
getLatestValidators() { - return voteTallyCache.getVoteTallyAtHead().getValidators(); + return validatorProvider.getValidatorsAtHead(); } } diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/statemachine/BftFinalState.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/statemachine/BftFinalState.java index b9c620be698..86979bc9965 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/statemachine/BftFinalState.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/statemachine/BftFinalState.java @@ -14,7 +14,6 @@ */ package org.hyperledger.besu.consensus.common.bft.statemachine; -import org.hyperledger.besu.consensus.common.VoteTallyCache; import org.hyperledger.besu.consensus.common.bft.BftHelpers; import org.hyperledger.besu.consensus.common.bft.BlockTimer; import org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier; @@ -22,6 +21,7 @@ import org.hyperledger.besu.consensus.common.bft.blockcreation.BftBlockCreatorFactory; import org.hyperledger.besu.consensus.common.bft.blockcreation.ProposerSelector; import org.hyperledger.besu.consensus.common.bft.network.ValidatorMulticaster; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.crypto.NodeKey; import org.hyperledger.besu.ethereum.core.Address; @@ -30,7 +30,7 @@ /** This is the full data set, or context, required for many of the aspects of BFT workflows. */ public class BftFinalState { - private final VoteTallyCache voteTallyCache; + private final ValidatorProvider validatorProvider; private final NodeKey nodeKey; private final Address localAddress; private final ProposerSelector proposerSelector; @@ -41,7 +41,7 @@ public class BftFinalState { private final ValidatorMulticaster validatorMulticaster; public BftFinalState( - final VoteTallyCache voteTallyCache, + final ValidatorProvider validatorProvider, final NodeKey nodeKey, final Address localAddress, final ProposerSelector proposerSelector, @@ -50,7 +50,7 @@ public BftFinalState( final BlockTimer blockTimer, final BftBlockCreatorFactory blockCreatorFactory, final Clock clock) { - this.voteTallyCache = voteTallyCache; + this.validatorProvider = validatorProvider; this.nodeKey = nodeKey; this.localAddress = localAddress; this.proposerSelector = proposerSelector; @@ -66,7 +66,7 @@ public int getQuorum() { } public Collection
getValidators() { - return voteTallyCache.getVoteTallyAtHead().getValidators(); + return validatorProvider.getValidatorsAtHead(); } public NodeKey getNodeKey() { diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/jsonrpc/AbstractGetSignerMetricsMethod.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/jsonrpc/AbstractGetSignerMetricsMethod.java index 541d8c026a7..46cbf5cd9f0 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/jsonrpc/AbstractGetSignerMetricsMethod.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/jsonrpc/AbstractGetSignerMetricsMethod.java @@ -15,7 +15,7 @@ package org.hyperledger.besu.consensus.common.jsonrpc; import org.hyperledger.besu.consensus.common.BlockInterface; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; @@ -37,15 +37,15 @@ public abstract class AbstractGetSignerMetricsMethod { private static final long DEFAULT_RANGE_BLOCK = 100; - private final VoteTallyCache voteTallyCache; + private final ValidatorProvider validatorProvider; private final BlockInterface blockInterface; private final BlockchainQueries blockchainQueries; protected AbstractGetSignerMetricsMethod( - final VoteTallyCache voteTallyCache, + final ValidatorProvider validatorProvider, final BlockInterface blockInterface, final BlockchainQueries blockchainQueries) { - this.voteTallyCache = voteTallyCache; + this.validatorProvider = validatorProvider; this.blockInterface = blockInterface; this.blockchainQueries = blockchainQueries; } @@ -88,9 +88,8 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { // Get All validators present in the last block of the range even // if they didn't propose a block if (currentIndex == lastBlockIndex) { - voteTallyCache - .getVoteTallyAfterBlock(header) - .getValidators() + validatorProvider + .getValidatorsAfterBlock(header) .forEach( address -> proposersMap.computeIfAbsent(address, SignerMetricResult::new)); diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/jsonrpc/AbstractVoteProposerMethod.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/jsonrpc/AbstractVoteProposerMethod.java index fb5d08b5c1f..12235b2f3b4 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/jsonrpc/AbstractVoteProposerMethod.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/jsonrpc/AbstractVoteProposerMethod.java @@ -14,9 +14,11 @@ */ package org.hyperledger.besu.consensus.common.jsonrpc; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteType; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; @@ -25,20 +27,25 @@ public class AbstractVoteProposerMethod { - private final VoteProposer voteProposer; + private final ValidatorProvider validatorProvider; - public AbstractVoteProposerMethod(final VoteProposer voteProposer) { - this.voteProposer = voteProposer; + public AbstractVoteProposerMethod(final ValidatorProvider validatorProvider) { + this.validatorProvider = validatorProvider; } public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { - final Map proposals = - voteProposer.getProposals().entrySet().stream() - .collect( - Collectors.toMap( - proposal -> proposal.getKey().toString(), - proposal -> proposal.getValue() == VoteType.ADD)); + if (validatorProvider.getVoteProvider().isPresent()) { + final Map proposals = + validatorProvider.getVoteProvider().get().getProposals().entrySet().stream() + .collect( + Collectors.toMap( + proposal -> proposal.getKey().toString(), + proposal -> proposal.getValue() == VoteType.ADD)); - return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), proposals); + return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), proposals); + } else { + return new JsonRpcErrorResponse( + requestContext.getRequest().getId(), JsonRpcError.METHOD_NOT_ENABLED); + } } } diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/ValidatorProvider.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/ValidatorProvider.java similarity index 71% rename from consensus/common/src/main/java/org/hyperledger/besu/consensus/common/ValidatorProvider.java rename to consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/ValidatorProvider.java index 3ae90fad2e0..1e38712be8c 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/ValidatorProvider.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/ValidatorProvider.java @@ -12,14 +12,19 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator; import org.hyperledger.besu.ethereum.core.Address; +import org.hyperledger.besu.ethereum.core.BlockHeader; import java.util.Collection; +import java.util.Optional; public interface ValidatorProvider { - // Returns the current list of validators - Collection
getValidators(); + Collection
getValidatorsAtHead(); + + Collection
getValidatorsAfterBlock(final BlockHeader header); + + Optional getVoteProvider(); } diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/ValidatorVote.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/ValidatorVote.java similarity index 93% rename from consensus/common/src/main/java/org/hyperledger/besu/consensus/common/ValidatorVote.java rename to consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/ValidatorVote.java index d10a7e2c611..1d8c253e71c 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/ValidatorVote.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/ValidatorVote.java @@ -12,10 +12,10 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator; import static com.google.common.base.Preconditions.checkNotNull; -import static org.hyperledger.besu.consensus.common.VoteType.ADD; +import static org.hyperledger.besu.consensus.common.validator.VoteType.ADD; import org.hyperledger.besu.ethereum.core.Address; diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/VoteProvider.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/VoteProvider.java new file mode 100644 index 00000000000..e3733ffd82a --- /dev/null +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/VoteProvider.java @@ -0,0 +1,34 @@ +/* + * Copyright ConsenSys AG. + * + * 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.common.validator; + +import org.hyperledger.besu.ethereum.core.Address; +import org.hyperledger.besu.ethereum.core.BlockHeader; + +import java.util.Map; +import java.util.Optional; + +public interface VoteProvider { + + Optional getVoteAfterBlock(final BlockHeader header, final Address localAddress); + + void authVote(final Address address); + + void dropVote(final Address address); + + void discardVote(final Address address); + + Map getProposals(); +} diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteType.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/VoteType.java similarity index 93% rename from consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteType.java rename to consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/VoteType.java index 3b43ef1303b..a9a113d4735 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteType.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/VoteType.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator; /** * Determines if a validator vote is indicating that they should be added, or removed. This does not diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/BlockValidatorProvider.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/BlockValidatorProvider.java new file mode 100644 index 00000000000..f570f20e855 --- /dev/null +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/BlockValidatorProvider.java @@ -0,0 +1,84 @@ +/* + * Copyright ConsenSys AG. + * + * 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.common.validator.blockbased; + +import org.hyperledger.besu.consensus.common.BftValidatorOverrides; +import org.hyperledger.besu.consensus.common.BlockInterface; +import org.hyperledger.besu.consensus.common.EpochManager; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.VoteProvider; +import org.hyperledger.besu.ethereum.chain.Blockchain; +import org.hyperledger.besu.ethereum.core.Address; +import org.hyperledger.besu.ethereum.core.BlockHeader; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class BlockValidatorProvider implements ValidatorProvider { + + private final VoteTallyCache voteTallyCache; + private final VoteProvider voteProvider; + + public static ValidatorProvider forkingValidatorProvider( + final Blockchain blockchain, + final EpochManager epochManager, + final BlockInterface blockInterface, + final Map> validatorForkMap) { + return new BlockValidatorProvider(blockchain, epochManager, blockInterface, validatorForkMap); + } + + public static ValidatorProvider nonForkingValidatorProvider( + final Blockchain blockchain, + final EpochManager epochManager, + final BlockInterface blockInterface) { + return new BlockValidatorProvider( + blockchain, epochManager, blockInterface, Collections.emptyMap()); + } + + private BlockValidatorProvider( + final Blockchain blockchain, + final EpochManager epochManager, + final BlockInterface blockInterface, + final Map> validatorForkMap) { + final VoteTallyUpdater voteTallyUpdater = new VoteTallyUpdater(epochManager, blockInterface); + final VoteProposer voteProposer = new VoteProposer(); + this.voteTallyCache = + new ForkingVoteTallyCache( + blockchain, + voteTallyUpdater, + epochManager, + blockInterface, + new BftValidatorOverrides(validatorForkMap)); + this.voteProvider = new BlockVoteProvider(voteTallyCache, voteProposer); + } + + @Override + public Collection
getValidatorsAtHead() { + return voteTallyCache.getVoteTallyAtHead().getValidators(); + } + + @Override + public Collection
getValidatorsAfterBlock(final BlockHeader header) { + return voteTallyCache.getVoteTallyAfterBlock(header).getValidators(); + } + + @Override + public Optional getVoteProvider() { + return Optional.of(voteProvider); + } +} diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/BlockVoteProvider.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/BlockVoteProvider.java new file mode 100644 index 00000000000..6c2e18adccf --- /dev/null +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/BlockVoteProvider.java @@ -0,0 +1,62 @@ +/* + * Copyright ConsenSys AG. + * + * 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.common.validator.blockbased; + +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; +import org.hyperledger.besu.consensus.common.validator.VoteProvider; +import org.hyperledger.besu.consensus.common.validator.VoteType; +import org.hyperledger.besu.ethereum.core.Address; +import org.hyperledger.besu.ethereum.core.BlockHeader; + +import java.util.Map; +import java.util.Optional; + +public class BlockVoteProvider implements VoteProvider { + + private final VoteTallyCache voteTallyCache; + private final VoteProposer voteProposer; + + public BlockVoteProvider(final VoteTallyCache voteTallyCache, final VoteProposer voteProposer) { + this.voteTallyCache = voteTallyCache; + this.voteProposer = voteProposer; + } + + @Override + public Optional getVoteAfterBlock( + final BlockHeader header, final Address localAddress) { + final VoteTally voteTally = voteTallyCache.getVoteTallyAfterBlock(header); + return voteProposer.getVote(localAddress, voteTally); + } + + @Override + public void authVote(final Address address) { + voteProposer.auth(address); + } + + @Override + public void dropVote(final Address address) { + voteProposer.drop(address); + } + + @Override + public void discardVote(final Address address) { + voteProposer.discard(address); + } + + @Override + public Map getProposals() { + return voteProposer.getProposals(); + } +} diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/ForkingVoteTallyCache.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/ForkingVoteTallyCache.java similarity index 83% rename from consensus/common/src/main/java/org/hyperledger/besu/consensus/common/ForkingVoteTallyCache.java rename to consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/ForkingVoteTallyCache.java index 8a4afffeac7..3d4d7e34847 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/ForkingVoteTallyCache.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/ForkingVoteTallyCache.java @@ -12,14 +12,17 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator.blockbased; import static com.google.common.base.Preconditions.checkNotNull; +import org.hyperledger.besu.consensus.common.BftValidatorOverrides; +import org.hyperledger.besu.consensus.common.BlockInterface; +import org.hyperledger.besu.consensus.common.EpochManager; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.BlockHeader; -public class ForkingVoteTallyCache extends VoteTallyCache { +class ForkingVoteTallyCache extends VoteTallyCache { private final BftValidatorOverrides validatorOverrides; diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteProposer.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteProposer.java similarity index 95% rename from consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteProposer.java rename to consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteProposer.java index 7a1e8592cc4..4c384cb03ff 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteProposer.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteProposer.java @@ -12,8 +12,10 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator.blockbased; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.ethereum.core.Address; import java.util.ArrayList; @@ -25,7 +27,7 @@ import java.util.concurrent.atomic.AtomicInteger; /** Container for pending votes and selecting a vote for new blocks */ -public class VoteProposer { +class VoteProposer { private final Map proposals = new ConcurrentHashMap<>(); private final AtomicInteger votePosition = new AtomicInteger(0); @@ -61,10 +63,6 @@ public Map getProposals() { return proposals; } - public Optional get(final Address address) { - return Optional.ofNullable(proposals.get(address)); - } - private boolean voteNotYetCast( final Address localAddress, final Address voteAddress, diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteTally.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTally.java similarity index 89% rename from consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteTally.java rename to consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTally.java index 05b4d41dc8c..7a3cefd2219 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteTally.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTally.java @@ -12,8 +12,9 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator.blockbased; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; import org.hyperledger.besu.ethereum.core.Address; import java.util.Collection; @@ -29,14 +30,14 @@ import com.google.common.collect.Maps; /** Tracks the current list of validators and votes to add or drop validators. */ -public class VoteTally implements ValidatorProvider { +class VoteTally { private final NavigableSet
currentValidators; private final Map> addVotesBySubject; private final Map> removeVotesBySubject; - public VoteTally(final Collection
initialValidators) { + VoteTally(final Collection
initialValidators) { this(new TreeSet<>(initialValidators), new HashMap<>(), new HashMap<>()); } @@ -55,7 +56,7 @@ private VoteTally( * * @param validatorVote The vote which was cast in a block header. */ - public void addVote(final ValidatorVote validatorVote) { + void addVote(final ValidatorVote validatorVote) { final Set
addVotesForSubject = addVotesBySubject.computeIfAbsent(validatorVote.getRecipient(), target -> new HashSet<>()); final Set
removeVotesForSubject = @@ -88,11 +89,11 @@ private void discardOutstandingVotesFor(final Address subject) { removeVotesBySubject.remove(subject); } - public Set
getOutstandingAddVotesFor(final Address subject) { + Set
getOutstandingAddVotesFor(final Address subject) { return Optional.ofNullable(addVotesBySubject.get(subject)).orElse(Collections.emptySet()); } - public Set
getOutstandingRemoveVotesFor(final Address subject) { + Set
getOutstandingRemoveVotesFor(final Address subject) { return Optional.ofNullable(removeVotesBySubject.get(subject)).orElse(Collections.emptySet()); } @@ -104,7 +105,7 @@ private int validatorLimit() { * Reset the outstanding vote tallies as required at each epoch. The current validator list is * unaffected. */ - public void discardOutstandingVotes() { + void discardOutstandingVotes() { addVotesBySubject.clear(); } @@ -112,12 +113,11 @@ public void discardOutstandingVotes() { * @return The collection of validators after the voting at the most recent block has been * finalised. */ - @Override - public Collection
getValidators() { + Collection
getValidators() { return currentValidators; } - public VoteTally copy() { + VoteTally copy() { final Map> addVotesBySubject = Maps.newHashMap(); final Map> removeVotesBySubject = Maps.newHashMap(); diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteTallyCache.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyCache.java similarity index 93% rename from consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteTallyCache.java rename to consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyCache.java index f1c4ad955e7..82fdc77f012 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteTallyCache.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyCache.java @@ -12,10 +12,12 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator.blockbased; import static com.google.common.base.Preconditions.checkNotNull; +import org.hyperledger.besu.consensus.common.BlockInterface; +import org.hyperledger.besu.consensus.common.EpochManager; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.Hash; @@ -28,7 +30,7 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; -public class VoteTallyCache { +class VoteTallyCache { private final Blockchain blockchain; private final EpochManager epochManager; @@ -38,7 +40,7 @@ public class VoteTallyCache { CacheBuilder.newBuilder().maximumSize(100).build(); private final BlockInterface blockInterface; - public VoteTallyCache( + VoteTallyCache( final Blockchain blockchain, final VoteTallyUpdater voteTallyUpdater, final EpochManager epochManager, @@ -54,7 +56,7 @@ public VoteTallyCache( this.blockInterface = blockInterface; } - public VoteTally getVoteTallyAtHead() { + VoteTally getVoteTallyAtHead() { return getVoteTallyAfterBlock(blockchain.getChainHeadHeader()); } @@ -69,7 +71,7 @@ public VoteTally getVoteTallyAtHead() { * @return The Vote Tally (and therefore validators) following the application of all votes upto * and including the requested header. */ - public VoteTally getVoteTallyAfterBlock(final BlockHeader header) { + VoteTally getVoteTallyAfterBlock(final BlockHeader header) { try { return voteTallyCache.get(header.getHash(), () -> populateCacheUptoAndIncluding(header)); } catch (final ExecutionException ex) { diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteTallyUpdater.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyUpdater.java similarity index 83% rename from consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteTallyUpdater.java rename to consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyUpdater.java index cd08704464b..55c4578bb86 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/VoteTallyUpdater.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyUpdater.java @@ -12,8 +12,11 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator.blockbased; +import org.hyperledger.besu.consensus.common.BlockInterface; +import org.hyperledger.besu.consensus.common.EpochManager; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.core.BlockHeader; @@ -28,14 +31,14 @@ * Provides the logic to extract vote tally state from the blockchain and update it as blocks are * added. */ -public class VoteTallyUpdater { +class VoteTallyUpdater { private static final Logger LOG = LogManager.getLogger(); private final EpochManager epochManager; private final BlockInterface blockInterface; - public VoteTallyUpdater(final EpochManager epochManager, final BlockInterface blockInterface) { + VoteTallyUpdater(final EpochManager epochManager, final BlockInterface blockInterface) { this.epochManager = epochManager; this.blockInterface = blockInterface; } @@ -46,7 +49,7 @@ public VoteTallyUpdater(final EpochManager epochManager, final BlockInterface bl * @param blockchain the blockchain to load the current state from * @return a VoteTally reflecting the state of the blockchain head */ - public VoteTally buildVoteTallyFromBlockchain(final Blockchain blockchain) { + VoteTally buildVoteTallyFromBlockchain(final Blockchain blockchain) { final long chainHeadBlockNumber = blockchain.getChainHeadBlockNumber(); final long epochBlockNumber = epochManager.getLastEpochBlock(chainHeadBlockNumber); LOG.info("Loading validator voting state starting from block {}", epochBlockNumber); @@ -67,7 +70,7 @@ public VoteTally buildVoteTallyFromBlockchain(final Blockchain blockchain) { * @param header the header of the block being added * @param voteTally the vote tally to update */ - public void updateForBlock(final BlockHeader header, final VoteTally voteTally) { + void updateForBlock(final BlockHeader header, final VoteTally voteTally) { if (epochManager.isEpochBlock(header.getNumber())) { voteTally.discardOutstandingVotes(); return; diff --git a/consensus/common/src/test-support/java/org/hyperledger/besu/consensus/common/bft/BftContextBuilder.java b/consensus/common/src/test-support/java/org/hyperledger/besu/consensus/common/bft/BftContextBuilder.java index cc22e668d51..302a0782a6c 100644 --- a/consensus/common/src/test-support/java/org/hyperledger/besu/consensus/common/bft/BftContextBuilder.java +++ b/consensus/common/src/test-support/java/org/hyperledger/besu/consensus/common/bft/BftContextBuilder.java @@ -19,9 +19,7 @@ import static org.mockito.Mockito.when; import static org.mockito.Mockito.withSettings; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.core.Address; import java.util.Collection; @@ -30,14 +28,12 @@ public class BftContextBuilder { public static BftContext setupContextWithValidators(final Collection
validators) { final BftContext bftContext = mock(BftContext.class, withSettings().lenient()); - final VoteTallyCache mockCache = mock(VoteTallyCache.class, withSettings().lenient()); - final VoteTally mockVoteTally = mock(VoteTally.class, withSettings().lenient()); + final ValidatorProvider mockValidatorProvider = + mock(ValidatorProvider.class, withSettings().lenient()); final BftBlockInterface mockBftBlockInterface = mock(BftBlockInterface.class, withSettings().lenient()); - when(bftContext.getVoteTallyCache()).thenReturn(mockCache); - when(mockCache.getVoteTallyAfterBlock(any())).thenReturn(mockVoteTally); - when(mockVoteTally.getValidators()).thenReturn(validators); - when(bftContext.getVoteProposer()).thenReturn(new VoteProposer()); + when(bftContext.getValidatorProvider()).thenReturn(mockValidatorProvider); + when(mockValidatorProvider.getValidatorsAfterBlock(any())).thenReturn(validators); when(bftContext.getBlockInterface()).thenReturn(mockBftBlockInterface); return bftContext; } @@ -45,14 +41,12 @@ public static BftContext setupContextWithValidators(final Collection
va public static BftContext setupContextWithBftExtraData( final Collection
validators, final BftExtraData bftExtraData) { final BftContext bftContext = mock(BftContext.class, withSettings().lenient()); - final VoteTallyCache mockCache = mock(VoteTallyCache.class, withSettings().lenient()); - final VoteTally mockVoteTally = mock(VoteTally.class, withSettings().lenient()); + final ValidatorProvider mockValidatorProvider = + mock(ValidatorProvider.class, withSettings().lenient()); final BftBlockInterface mockBftBlockInterface = mock(BftBlockInterface.class, withSettings().lenient()); - when(bftContext.getVoteTallyCache()).thenReturn(mockCache); - when(mockCache.getVoteTallyAfterBlock(any())).thenReturn(mockVoteTally); - when(mockVoteTally.getValidators()).thenReturn(validators); - when(bftContext.getVoteProposer()).thenReturn(new VoteProposer()); + when(bftContext.getValidatorProvider()).thenReturn(mockValidatorProvider); + when(mockValidatorProvider.getValidatorsAfterBlock(any())).thenReturn(validators); when(bftContext.getBlockInterface()).thenReturn(mockBftBlockInterface); when(mockBftBlockInterface.getExtraData(any())).thenReturn(bftExtraData); return bftContext; @@ -61,12 +55,10 @@ public static BftContext setupContextWithBftExtraData( public static BftContext setupContextWithBftExtraDataEncoder( final Collection
validators, final BftExtraDataCodec bftExtraDataCodec) { final BftContext bftContext = mock(BftContext.class, withSettings().lenient()); - final VoteTallyCache mockCache = mock(VoteTallyCache.class, withSettings().lenient()); - final VoteTally mockVoteTally = mock(VoteTally.class, withSettings().lenient()); - when(bftContext.getVoteTallyCache()).thenReturn(mockCache); - when(mockCache.getVoteTallyAfterBlock(any())).thenReturn(mockVoteTally); - when(mockVoteTally.getValidators()).thenReturn(validators); - when(bftContext.getVoteProposer()).thenReturn(new VoteProposer()); + final ValidatorProvider mockValidatorProvider = + mock(ValidatorProvider.class, withSettings().lenient()); + when(bftContext.getValidatorProvider()).thenReturn(mockValidatorProvider); + when(mockValidatorProvider.getValidatorsAfterBlock(any())).thenReturn(validators); when(bftContext.getBlockInterface()).thenReturn(new BftBlockInterface(bftExtraDataCodec)); return bftContext; diff --git a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/blockcreation/ProposerSelectorTest.java b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/blockcreation/ProposerSelectorTest.java index 5579a8149fd..0f1d77c31e5 100644 --- a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/blockcreation/ProposerSelectorTest.java +++ b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/blockcreation/ProposerSelectorTest.java @@ -21,9 +21,8 @@ import static org.mockito.Mockito.when; import org.hyperledger.besu.consensus.common.BlockInterface; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; import org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.chain.MutableBlockchain; import org.hyperledger.besu.ethereum.core.Address; @@ -41,13 +40,13 @@ public class ProposerSelectorTest { private final BlockInterface blockInterface = mock(BlockInterface.class); - private final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); + private final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); private Blockchain createMockedBlockChainWithHeadOf( final long blockNumber, final Address proposer, final Collection
validators) { when(blockInterface.getProposerOfBlock(any())).thenReturn(proposer); - when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validators)); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validators); final BlockHeaderTestFixture headerBuilderFixture = new BlockHeaderTestFixture(); headerBuilderFixture.number(blockNumber); @@ -98,7 +97,7 @@ public void roundRobinChangesProposerOnRoundZeroOfNextBlock() { createMockedBlockChainWithHeadOf(PREV_BLOCK_NUMBER, localAddr, validatorList); final ProposerSelector uut = - new ProposerSelector(blockchain, blockInterface, true, voteTallyCache); + new ProposerSelector(blockchain, blockInterface, true, validatorProvider); final ConsensusRoundIdentifier roundId = new ConsensusRoundIdentifier(PREV_BLOCK_NUMBER + 1, 0); @@ -117,7 +116,7 @@ public void lastValidatorInListValidatedPreviousBlockSoFirstIsNextProposer() { createMockedBlockChainWithHeadOf(PREV_BLOCK_NUMBER, localAddr, validatorList); final ProposerSelector uut = - new ProposerSelector(blockchain, blockInterface, true, voteTallyCache); + new ProposerSelector(blockchain, blockInterface, true, validatorProvider); final ConsensusRoundIdentifier roundId = new ConsensusRoundIdentifier(PREV_BLOCK_NUMBER + 1, 0); @@ -137,7 +136,7 @@ public void stickyProposerDoesNotChangeOnRoundZeroOfNextBlock() { createMockedBlockChainWithHeadOf(PREV_BLOCK_NUMBER, localAddr, validatorList); final ProposerSelector uut = - new ProposerSelector(blockchain, blockInterface, false, voteTallyCache); + new ProposerSelector(blockchain, blockInterface, false, validatorProvider); final Address nextProposer = uut.selectProposerForRound(roundId); assertThat(nextProposer).isEqualTo(localAddr); @@ -155,7 +154,7 @@ public void stickyProposerChangesOnSubsequentRoundsAtSameBlockHeight() { createMockedBlockChainWithHeadOf(PREV_BLOCK_NUMBER, localAddr, validatorList); final ProposerSelector uut = - new ProposerSelector(blockchain, blockInterface, false, voteTallyCache); + new ProposerSelector(blockchain, blockInterface, false, validatorProvider); assertThat(uut.selectProposerForRound(roundId)).isEqualTo(localAddr); roundId = new ConsensusRoundIdentifier(PREV_BLOCK_NUMBER + 1, 1); @@ -182,7 +181,7 @@ public void whenProposerSelfRemovesSelectsNextProposerInLineEvenWhenSticky() { createMockedBlockChainWithHeadOf(PREV_BLOCK_NUMBER, localAddr, validatorList); final ProposerSelector uut = - new ProposerSelector(blockchain, blockInterface, false, voteTallyCache); + new ProposerSelector(blockchain, blockInterface, false, validatorProvider); assertThat(uut.selectProposerForRound(roundId)).isEqualTo(validatorList.get(2)); } @@ -203,7 +202,7 @@ public void whenProposerSelfRemovesSelectsNextProposerInLineEvenWhenRoundRobin() createMockedBlockChainWithHeadOf(PREV_BLOCK_NUMBER, localAddr, validatorList); final ProposerSelector uut = - new ProposerSelector(blockchain, blockInterface, true, voteTallyCache); + new ProposerSelector(blockchain, blockInterface, true, validatorProvider); assertThat(uut.selectProposerForRound(roundId)).isEqualTo(validatorList.get(2)); } @@ -225,7 +224,7 @@ public void proposerSelfRemovesAndHasHighestAddressNewProposerIsFirstInList() { createMockedBlockChainWithHeadOf(PREV_BLOCK_NUMBER, localAddr, validatorList); final ProposerSelector uut = - new ProposerSelector(blockchain, blockInterface, false, voteTallyCache); + new ProposerSelector(blockchain, blockInterface, false, validatorProvider); assertThat(uut.selectProposerForRound(roundId)).isEqualTo(validatorList.get(0)); } diff --git a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/network/ValidatorPeersTest.java b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/network/ValidatorPeersTest.java index b23b792c8e3..f8dd8a35594 100644 --- a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/network/ValidatorPeersTest.java +++ b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/network/ValidatorPeersTest.java @@ -22,8 +22,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.crypto.SECPPublicKey; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; import org.hyperledger.besu.ethereum.core.Address; @@ -52,7 +51,7 @@ public class ValidatorPeersTest { private final List publicKeys = newArrayList(); private final List peerConnections = newArrayList(); - @Mock private VoteTallyCache voteTallyCache; + @Mock ValidatorProvider validatorProvider; @Before public void setup() { @@ -66,9 +65,7 @@ public void setup() { peerConnections.add(peerConnection); } - final VoteTally validatorProvider = mock(VoteTally.class); - when(voteTallyCache.getVoteTallyAtHead()).thenReturn(validatorProvider); - when(validatorProvider.getValidators()).thenReturn(validators); + when(validatorProvider.getValidatorsAtHead()).thenReturn(validators); } private PeerConnection mockPeerConnection(final Address address) { @@ -84,7 +81,7 @@ public void onlyValidatorsAreSentAMessage() throws PeerNotConnected { // Only add the first Peer's address to the validators. validators.add(Util.publicKeyToAddress(publicKeys.get(0))); - final ValidatorPeers peers = new ValidatorPeers(voteTallyCache, PROTOCOL_NAME); + final ValidatorPeers peers = new ValidatorPeers(validatorProvider, PROTOCOL_NAME); for (final PeerConnection peer : peerConnections) { peers.add(peer); } @@ -104,7 +101,7 @@ public void handlesDuplicateConnection() throws PeerNotConnected { validators.add(peer0Address); final PeerConnection duplicatePeer = mockPeerConnection(peer0Address); - final ValidatorPeers peers = new ValidatorPeers(voteTallyCache, PROTOCOL_NAME); + final ValidatorPeers peers = new ValidatorPeers(validatorProvider, PROTOCOL_NAME); for (final PeerConnection peer : peerConnections) { peers.add(peer); } @@ -126,7 +123,7 @@ public void handlesTransientDuplicateConnection() throws PeerNotConnected { validators.add(peer0Address); final PeerConnection duplicatePeer = mockPeerConnection(peer0Address); - final ValidatorPeers peers = new ValidatorPeers(voteTallyCache, PROTOCOL_NAME); + final ValidatorPeers peers = new ValidatorPeers(validatorProvider, PROTOCOL_NAME); for (final PeerConnection peer : peerConnections) { peers.add(peer); } @@ -147,7 +144,7 @@ public void handlesTransientDuplicateConnection() throws PeerNotConnected { public void doesntSendToValidatorsWhichAreNotDirectlyConnected() throws PeerNotConnected { validators.add(Util.publicKeyToAddress(publicKeys.get(0))); - final ValidatorPeers peers = new ValidatorPeers(voteTallyCache, PROTOCOL_NAME); + final ValidatorPeers peers = new ValidatorPeers(validatorProvider, PROTOCOL_NAME); // only add peer connections 1, 2 & 3, none of which should be invoked. newArrayList(1, 2, 3).forEach(i -> peers.add(peerConnections.get(i))); @@ -168,7 +165,7 @@ public void onlyValidatorsAreSentAMessageNotInExcludes() throws PeerNotConnected validators.add(validatorAddress); validators.add(Util.publicKeyToAddress(publicKeys.get(1))); - final ValidatorPeers peers = new ValidatorPeers(voteTallyCache, PROTOCOL_NAME); + final ValidatorPeers peers = new ValidatorPeers(validatorProvider, PROTOCOL_NAME); for (final PeerConnection peer : peerConnections) { peers.add(peer); } diff --git a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/jsonrpc/AbstractVoteProposerMethodTest.java b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/jsonrpc/AbstractVoteProposerMethodTest.java index fc35e9960c1..de2f7824cfc 100644 --- a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/jsonrpc/AbstractVoteProposerMethodTest.java +++ b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/jsonrpc/AbstractVoteProposerMethodTest.java @@ -18,28 +18,32 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteType; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.VoteProvider; +import org.hyperledger.besu.consensus.common.validator.VoteType; 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.response.JsonRpcResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.core.Address; +import java.util.Optional; + import com.google.common.collect.ImmutableMap; import org.junit.Test; public abstract class AbstractVoteProposerMethodTest { - private final VoteProposer voteProposer = mock(VoteProposer.class); + private final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + private final VoteProvider voteProvider = mock(VoteProvider.class); private final String JSON_RPC_VERSION = "2.0"; protected abstract AbstractVoteProposerMethod getMethod(); protected abstract String getMethodName(); - protected VoteProposer getVoteProposer() { - return voteProposer; + protected ValidatorProvider getValidatorProvider() { + return validatorProvider; } @Test @@ -48,7 +52,8 @@ public void testConversionFromVoteTypeToBoolean() { new JsonRpcRequestContext( new JsonRpcRequest(JSON_RPC_VERSION, getMethodName(), new Object[] {})); - when(voteProposer.getProposals()) + when(validatorProvider.getVoteProvider()).thenReturn(Optional.of(voteProvider)); + when(voteProvider.getProposals()) .thenReturn( ImmutableMap.of( Address.fromHexString("1"), diff --git a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/ForkingVoteTallyCacheTest.java b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/ForkingVoteTallyCacheTest.java similarity index 95% rename from consensus/common/src/test/java/org/hyperledger/besu/consensus/common/ForkingVoteTallyCacheTest.java rename to consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/ForkingVoteTallyCacheTest.java index 1e4cbba4737..d35267e5d29 100644 --- a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/ForkingVoteTallyCacheTest.java +++ b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/ForkingVoteTallyCacheTest.java @@ -12,11 +12,13 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator.blockbased; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; +import org.hyperledger.besu.consensus.common.BftValidatorOverrides; +import org.hyperledger.besu.consensus.common.EpochManager; import org.hyperledger.besu.ethereum.core.Address; import java.util.HashMap; diff --git a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteProposerTest.java b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteProposerTest.java similarity index 97% rename from consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteProposerTest.java rename to consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteProposerTest.java index f5a41c1b317..40af8ee25da 100644 --- a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteProposerTest.java +++ b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteProposerTest.java @@ -12,10 +12,12 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator.blockbased; import static org.assertj.core.api.Assertions.assertThat; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.ethereum.core.Address; import java.util.Arrays; diff --git a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteTallyCacheTest.java b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyCacheTest.java similarity index 95% rename from consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteTallyCacheTest.java rename to consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyCacheTest.java index 28ebbc9b6a9..d7187a037fe 100644 --- a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteTallyCacheTest.java +++ b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyCacheTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator.blockbased; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -24,6 +24,9 @@ import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; +import org.hyperledger.besu.consensus.common.EpochManager; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.ethereum.core.Block; import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.Hash; diff --git a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteTallyCacheTestBase.java b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyCacheTestBase.java similarity index 95% rename from consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteTallyCacheTestBase.java rename to consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyCacheTestBase.java index 090a54f2abd..6fdbb574e02 100644 --- a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteTallyCacheTestBase.java +++ b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyCacheTestBase.java @@ -12,13 +12,14 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator.blockbased; import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import org.hyperledger.besu.consensus.common.BlockInterface; import org.hyperledger.besu.ethereum.chain.MutableBlockchain; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.core.AddressHelpers; diff --git a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteTallyTest.java b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyTest.java similarity index 98% rename from consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteTallyTest.java rename to consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyTest.java index 515497be20c..bb5b9abe8e6 100644 --- a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteTallyTest.java +++ b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyTest.java @@ -12,12 +12,14 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator.blockbased; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.ethereum.core.Address; import org.junit.Test; diff --git a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteTallyUpdaterTest.java b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyUpdaterTest.java similarity index 94% rename from consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteTallyUpdaterTest.java rename to consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyUpdaterTest.java index cf0f5d2e156..8a69120997a 100644 --- a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/VoteTallyUpdaterTest.java +++ b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/validator/blockbased/VoteTallyUpdaterTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.consensus.common; +package org.hyperledger.besu.consensus.common.validator.blockbased; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; @@ -23,6 +23,10 @@ import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; +import org.hyperledger.besu.consensus.common.BlockInterface; +import org.hyperledger.besu.consensus.common.EpochManager; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; import org.hyperledger.besu.ethereum.chain.MutableBlockchain; diff --git a/consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/TestContextBuilder.java b/consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/TestContextBuilder.java index 9cce16ddc02..44f265dc629 100644 --- a/consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/TestContextBuilder.java +++ b/consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/TestContextBuilder.java @@ -21,9 +21,6 @@ import org.hyperledger.besu.config.StubGenesisConfigOptions; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTallyCache; -import org.hyperledger.besu.consensus.common.VoteTallyUpdater; import org.hyperledger.besu.consensus.common.bft.BftBlockHeaderFunctions; import org.hyperledger.besu.consensus.common.bft.BftBlockInterface; import org.hyperledger.besu.consensus.common.bft.BftContext; @@ -49,6 +46,8 @@ import org.hyperledger.besu.consensus.common.bft.statemachine.BftEventHandler; import org.hyperledger.besu.consensus.common.bft.statemachine.BftFinalState; import org.hyperledger.besu.consensus.common.bft.statemachine.FutureMessageBuffer; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider; import org.hyperledger.besu.consensus.ibft.IbftBlockHeaderValidationRulesetFactory; import org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec; import org.hyperledger.besu.consensus.ibft.IbftGossip; @@ -305,20 +304,15 @@ private static ControllerAndState createControllerAndFinalState( final BftBlockInterface blockInterface = new BftBlockInterface(IBFT_EXTRA_DATA_ENCODER); - final VoteTallyCache voteTallyCache = - new VoteTallyCache( - blockChain, - new VoteTallyUpdater(epochManager, blockInterface), - epochManager, - new BftBlockInterface(IBFT_EXTRA_DATA_ENCODER)); - - final VoteProposer voteProposer = new VoteProposer(); + final ValidatorProvider validatorProvider = + BlockValidatorProvider.nonForkingValidatorProvider( + blockChain, epochManager, blockInterface); final ProtocolContext protocolContext = new ProtocolContext( blockChain, worldStateArchive, - new BftContext(voteTallyCache, voteProposer, epochManager, blockInterface)); + new BftContext(validatorProvider, epochManager, blockInterface)); final PendingTransactions pendingTransactions = new PendingTransactions( @@ -343,12 +337,12 @@ private static ControllerAndState createControllerAndFinalState( IBFT_EXTRA_DATA_ENCODER); final ProposerSelector proposerSelector = - new ProposerSelector(blockChain, blockInterface, true, voteTallyCache); + new ProposerSelector(blockChain, blockInterface, true, validatorProvider); final BftExecutors bftExecutors = BftExecutors.create(new NoOpMetricsSystem()); final BftFinalState finalState = new BftFinalState( - protocolContext.getConsensusState(BftContext.class).getVoteTallyCache(), + protocolContext.getConsensusState(BftContext.class).getValidatorProvider(), nodeKey, Util.publicKeyToAddress(nodeKey.getPublicKey()), proposerSelector, diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftExtraDataCodec.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftExtraDataCodec.java index d578ae292f9..bf97077c657 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftExtraDataCodec.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftExtraDataCodec.java @@ -17,10 +17,10 @@ import static org.hyperledger.besu.consensus.common.bft.Vote.ADD_BYTE_VALUE; import static org.hyperledger.besu.consensus.common.bft.Vote.DROP_BYTE_VALUE; -import org.hyperledger.besu.consensus.common.VoteType; import org.hyperledger.besu.consensus.common.bft.BftExtraData; import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec; import org.hyperledger.besu.consensus.common.bft.Vote; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.crypto.SECPSignature; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; import org.hyperledger.besu.ethereum.core.Address; diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftLegacyContext.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftLegacyContext.java index 010684f703a..e96d5422ebf 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftLegacyContext.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/IbftLegacyContext.java @@ -17,34 +17,26 @@ import org.hyperledger.besu.consensus.common.BlockInterface; import org.hyperledger.besu.consensus.common.EpochManager; import org.hyperledger.besu.consensus.common.PoaContext; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; /** Holds the BFT specific mutable state. */ public class IbftLegacyContext implements PoaContext { - private final VoteTallyCache voteTallyCache; - private final VoteProposer voteProposer; + private final ValidatorProvider validatorProvider; private final EpochManager epochManager; private final BlockInterface blockInterface; public IbftLegacyContext( - final VoteTallyCache voteTallyCache, - final VoteProposer voteProposer, + final ValidatorProvider validatorProvider, final EpochManager epochManager, final BlockInterface blockInterface) { - this.voteTallyCache = voteTallyCache; - this.voteProposer = voteProposer; + this.validatorProvider = validatorProvider; this.epochManager = epochManager; this.blockInterface = blockInterface; } - public VoteTallyCache getVoteTallyCache() { - return voteTallyCache; - } - - public VoteProposer getVoteProposer() { - return voteProposer; + public ValidatorProvider getValidatorProvider() { + return validatorProvider; } public EpochManager getEpochManager() { diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethods.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethods.java index 2de7efb51f0..e223b3ac320 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethods.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethods.java @@ -16,11 +16,10 @@ import org.hyperledger.besu.consensus.common.BlockInterface; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTallyCache; -import org.hyperledger.besu.consensus.common.VoteTallyUpdater; import org.hyperledger.besu.consensus.common.bft.BftBlockInterface; import org.hyperledger.besu.consensus.common.bft.BftContext; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider; import org.hyperledger.besu.consensus.ibft.jsonrpc.methods.IbftDiscardValidatorVote; import org.hyperledger.besu.consensus.ibft.jsonrpc.methods.IbftGetPendingVotes; import org.hyperledger.besu.consensus.ibft.jsonrpc.methods.IbftGetSignerMetrics; @@ -51,29 +50,33 @@ protected RpcApi getApiGroup() { @Override protected Map create() { + final MutableBlockchain blockchain = context.getBlockchain(); final BlockchainQueries blockchainQueries = - new BlockchainQueries(context.getBlockchain(), context.getWorldStateArchive()); + new BlockchainQueries(blockchain, context.getWorldStateArchive()); final BftContext bftContext = context.getConsensusState(BftContext.class); - final VoteProposer voteProposer = bftContext.getVoteProposer(); final BlockInterface blockInterface = bftContext.getBlockInterface(); + final ValidatorProvider validatorProvider = + context.getConsensusState(BftContext.class).getValidatorProvider(); - final VoteTallyCache voteTallyCache = createVoteTallyCache(context); + // Must create our own voteTallyCache as using this would pollute the main voteTallyCache + final ValidatorProvider readOnlyValidatorProvider = + createValidatorProvider(context, blockchain); return mapOf( - new IbftProposeValidatorVote(voteProposer), + new IbftProposeValidatorVote(validatorProvider), new IbftGetValidatorsByBlockNumber(blockchainQueries, blockInterface), - new IbftDiscardValidatorVote(voteProposer), - new IbftGetValidatorsByBlockHash(context.getBlockchain(), blockInterface), - new IbftGetSignerMetrics(voteTallyCache, blockInterface, blockchainQueries), - new IbftGetPendingVotes(voteProposer)); + new IbftDiscardValidatorVote(validatorProvider), + new IbftGetValidatorsByBlockHash(blockchain, blockInterface), + new IbftGetSignerMetrics(readOnlyValidatorProvider, blockInterface, blockchainQueries), + new IbftGetPendingVotes(validatorProvider)); } - private VoteTallyCache createVoteTallyCache(final ProtocolContext context) { + private ValidatorProvider createValidatorProvider( + final ProtocolContext context, final MutableBlockchain blockchain) { final BftContext bftContext = context.getConsensusState(BftContext.class); final EpochManager epochManager = bftContext.getEpochManager(); final BftBlockInterface bftBlockInterface = bftContext.getBlockInterface(); - final VoteTallyUpdater voteTallyUpdater = new VoteTallyUpdater(epochManager, bftBlockInterface); - final MutableBlockchain blockchain = context.getBlockchain(); - return new VoteTallyCache(blockchain, voteTallyUpdater, epochManager, bftBlockInterface); + return BlockValidatorProvider.nonForkingValidatorProvider( + blockchain, epochManager, bftBlockInterface); } } diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftDiscardValidatorVote.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftDiscardValidatorVote.java index 4afe6579b5a..bf24b7caec7 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftDiscardValidatorVote.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftDiscardValidatorVote.java @@ -14,7 +14,9 @@ */ package org.hyperledger.besu.consensus.ibft.jsonrpc.methods; -import org.hyperledger.besu.consensus.common.VoteProposer; +import static com.google.common.base.Preconditions.checkState; + +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; @@ -27,10 +29,10 @@ public class IbftDiscardValidatorVote implements JsonRpcMethod { private static final Logger LOG = LogManager.getLogger(); - private final VoteProposer voteProposer; + private final ValidatorProvider validatorProvider; - public IbftDiscardValidatorVote(final VoteProposer voteProposer) { - this.voteProposer = voteProposer; + public IbftDiscardValidatorVote(final ValidatorProvider validatorProvider) { + this.validatorProvider = validatorProvider; } @Override @@ -40,9 +42,10 @@ public String getName() { @Override public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { + checkState(validatorProvider.getVoteProvider().isPresent(), "Ibft requires a vote provider"); final Address validatorAddress = requestContext.getRequiredParameter(0, Address.class); LOG.trace("Received RPC rpcName={} address={}", getName(), validatorAddress); - voteProposer.discard(validatorAddress); + validatorProvider.getVoteProvider().get().discardVote(validatorAddress); return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), true); } diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetPendingVotes.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetPendingVotes.java index bdd3a13a1bb..6d48b806536 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetPendingVotes.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetPendingVotes.java @@ -14,15 +14,15 @@ */ package org.hyperledger.besu.consensus.ibft.jsonrpc.methods; -import org.hyperledger.besu.consensus.common.VoteProposer; import org.hyperledger.besu.consensus.common.jsonrpc.AbstractVoteProposerMethod; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; public class IbftGetPendingVotes extends AbstractVoteProposerMethod implements JsonRpcMethod { - public IbftGetPendingVotes(final VoteProposer voteProposer) { - super(voteProposer); + public IbftGetPendingVotes(final ValidatorProvider validatorProvider) { + super(validatorProvider); } @Override diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetSignerMetrics.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetSignerMetrics.java index 19d42737061..8b8e21a6cac 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetSignerMetrics.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetSignerMetrics.java @@ -15,8 +15,8 @@ package org.hyperledger.besu.consensus.ibft.jsonrpc.methods; import org.hyperledger.besu.consensus.common.BlockInterface; -import org.hyperledger.besu.consensus.common.VoteTallyCache; import org.hyperledger.besu.consensus.common.jsonrpc.AbstractGetSignerMetricsMethod; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; @@ -24,10 +24,10 @@ public class IbftGetSignerMetrics extends AbstractGetSignerMetricsMethod implements JsonRpcMethod { public IbftGetSignerMetrics( - final VoteTallyCache voteTallyCache, + final ValidatorProvider validatorProvider, final BlockInterface blockInterface, final BlockchainQueries blockchainQueries) { - super(voteTallyCache, blockInterface, blockchainQueries); + super(validatorProvider, blockInterface, blockchainQueries); } @Override diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftProposeValidatorVote.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftProposeValidatorVote.java index a8eed3c8b00..6d69f6d65b9 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftProposeValidatorVote.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftProposeValidatorVote.java @@ -14,8 +14,10 @@ */ package org.hyperledger.besu.consensus.ibft.jsonrpc.methods; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteType; +import static com.google.common.base.Preconditions.checkState; + +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; @@ -28,10 +30,10 @@ public class IbftProposeValidatorVote implements JsonRpcMethod { private static final Logger LOG = LogManager.getLogger(); - private final VoteProposer voteProposer; + private final ValidatorProvider validatorProvider; - public IbftProposeValidatorVote(final VoteProposer voteProposer) { - this.voteProposer = voteProposer; + public IbftProposeValidatorVote(final ValidatorProvider validatorProvider) { + this.validatorProvider = validatorProvider; } @Override @@ -41,7 +43,7 @@ public String getName() { @Override public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { - + checkState(validatorProvider.getVoteProvider().isPresent(), "Ibft requires a vote provider"); final Address validatorAddress = requestContext.getRequiredParameter(0, Address.class); final Boolean add = requestContext.getRequiredParameter(1, Boolean.class); LOG.trace( @@ -51,9 +53,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { validatorAddress); if (add) { - voteProposer.auth(validatorAddress); + validatorProvider.getVoteProvider().get().authVote(validatorAddress); } else { - voteProposer.drop(validatorAddress); + validatorProvider.getVoteProvider().get().dropVote(validatorAddress); } return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), true); diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidatorFactory.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidatorFactory.java index b22677410d5..a0bb4422480 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidatorFactory.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidatorFactory.java @@ -49,9 +49,8 @@ public MessageValidatorFactory( private Collection
getValidatorsAfterBlock(final BlockHeader parentHeader) { return protocolContext .getConsensusState(BftContext.class) - .getVoteTallyCache() - .getVoteTallyAfterBlock(parentHeader) - .getValidators(); + .getValidatorProvider() + .getValidatorsAfterBlock(parentHeader); } private SignedDataValidator createSignedDataValidator( diff --git a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftExtraDataEncoderTest.java b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftExtraDataEncoderTest.java index 7602949eb80..ada81268bac 100644 --- a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftExtraDataEncoderTest.java +++ b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftExtraDataEncoderTest.java @@ -17,9 +17,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import org.hyperledger.besu.consensus.common.VoteType; import org.hyperledger.besu.consensus.common.bft.BftExtraData; import org.hyperledger.besu.consensus.common.bft.Vote; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.crypto.SECPSignature; import org.hyperledger.besu.crypto.SignatureAlgorithm; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; diff --git a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftDiscardValidatorVoteTest.java b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftDiscardValidatorVoteTest.java index eb860e2b640..7d43367992a 100644 --- a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftDiscardValidatorVoteTest.java +++ b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftDiscardValidatorVoteTest.java @@ -17,8 +17,10 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -import org.hyperledger.besu.consensus.common.VoteProposer; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.VoteProvider; 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.exception.InvalidJsonRpcParameters; @@ -26,13 +28,16 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.core.Address; +import java.util.Optional; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; public class IbftDiscardValidatorVoteTest { - private final VoteProposer voteProposer = mock(VoteProposer.class); + private final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + private final VoteProvider voteProvider = mock(VoteProvider.class); private final String IBFT_METHOD = "ibft_discardValidatorVote"; private final String JSON_RPC_VERSION = "2.0"; private IbftDiscardValidatorVote method; @@ -41,7 +46,8 @@ public class IbftDiscardValidatorVoteTest { @Before public void setup() { - method = new IbftDiscardValidatorVote(voteProposer); + method = new IbftDiscardValidatorVote(validatorProvider); + when(validatorProvider.getVoteProvider()).thenReturn(Optional.of(voteProvider)); } @Test @@ -81,7 +87,7 @@ public void discardValidator() { assertThat(response).isEqualToComparingFieldByField(expectedResponse); - verify(voteProposer).discard(parameterAddress); + verify(voteProvider).discardVote(parameterAddress); } private JsonRpcRequestContext requestWithParams(final Object... params) { diff --git a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetPendingVotesTest.java b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetPendingVotesTest.java index 363785df111..f1f1fb62a2c 100644 --- a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetPendingVotesTest.java +++ b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetPendingVotesTest.java @@ -38,7 +38,7 @@ protected String getMethodName() { @Before public void setup() { - method = new IbftGetPendingVotes(getVoteProposer()); + method = new IbftGetPendingVotes(getValidatorProvider()); } @Test diff --git a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetSignerMetricsTest.java b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetSignerMetricsTest.java index b36a3ce1bb7..e84bacd55e1 100644 --- a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetSignerMetricsTest.java +++ b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetSignerMetricsTest.java @@ -22,8 +22,7 @@ import static org.mockito.Mockito.when; import org.hyperledger.besu.consensus.common.BlockInterface; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; 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.exception.InvalidJsonRpcParameters; @@ -56,7 +55,7 @@ public class IbftGetSignerMetricsTest { private final String JSON_RPC_VERSION = "2.0"; private IbftGetSignerMetrics method; - private VoteTallyCache voteTallyCache; + private ValidatorProvider validatorProvider; private BlockchainQueries blockchainQueries; private BlockInterface blockInterface; @@ -64,10 +63,10 @@ public class IbftGetSignerMetricsTest { @Before public void setup() { - voteTallyCache = mock(VoteTallyCache.class); + validatorProvider = mock(ValidatorProvider.class); blockchainQueries = mock(BlockchainQueries.class); blockInterface = mock(BlockInterface.class); - method = new IbftGetSignerMetrics(voteTallyCache, blockInterface, blockchainQueries); + method = new IbftGetSignerMetrics(validatorProvider, blockInterface, blockchainQueries); } @Test @@ -259,8 +258,7 @@ private SignerMetricResult generateBlock(final long number) { when(blockchainQueries.getBlockHeaderByNumber(number)).thenReturn(Optional.of(header)); when(blockInterface.getProposerOfBlock(header)).thenReturn(proposerAddressBlock); - when(voteTallyCache.getVoteTallyAfterBlock(header)) - .thenReturn(new VoteTally(Arrays.asList(VALIDATORS))); + when(validatorProvider.getValidatorsAfterBlock(header)).thenReturn(Arrays.asList(VALIDATORS)); final SignerMetricResult signerMetricResult = new SignerMetricResult(proposerAddressBlock); signerMetricResult.incrementeNbBlock(); diff --git a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftProposeValidatorVoteTest.java b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftProposeValidatorVoteTest.java index 5e56fe99fa0..0c3639a8b77 100644 --- a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftProposeValidatorVoteTest.java +++ b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftProposeValidatorVoteTest.java @@ -17,8 +17,10 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -import org.hyperledger.besu.consensus.common.VoteProposer; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.VoteProvider; 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.exception.InvalidJsonRpcParameters; @@ -26,13 +28,16 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.core.Address; +import java.util.Optional; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; public class IbftProposeValidatorVoteTest { - private final VoteProposer voteProposer = mock(VoteProposer.class); + private final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + private final VoteProvider voteProvider = mock(VoteProvider.class); private final String IBFT_METHOD = "ibft_proposeValidatorVote"; private final String JSON_RPC_VERSION = "2.0"; private IbftProposeValidatorVote method; @@ -41,7 +46,8 @@ public class IbftProposeValidatorVoteTest { @Before public void setup() { - method = new IbftProposeValidatorVote(voteProposer); + method = new IbftProposeValidatorVote(validatorProvider); + when(validatorProvider.getVoteProvider()).thenReturn(Optional.of(voteProvider)); } @Test @@ -100,7 +106,7 @@ public void addValidator() { assertThat(response).isEqualToComparingFieldByField(expectedResponse); - verify(voteProposer).auth(parameterAddress); + verify(voteProvider).authVote(parameterAddress); } @Test @@ -114,7 +120,7 @@ public void removeValidator() { assertThat(response).isEqualToComparingFieldByField(expectedResponse); - verify(voteProposer).drop(parameterAddress); + verify(voteProvider).dropVote(parameterAddress); } private JsonRpcRequestContext requestWithParams(final Object... params) { diff --git a/consensus/ibftlegacy/src/main/java/org/hyperledger/besu/consensus/ibftlegacy/IbftLegacyBlockInterface.java b/consensus/ibftlegacy/src/main/java/org/hyperledger/besu/consensus/ibftlegacy/IbftLegacyBlockInterface.java index c2eb45d2e8c..31b15bba9a6 100644 --- a/consensus/ibftlegacy/src/main/java/org/hyperledger/besu/consensus/ibftlegacy/IbftLegacyBlockInterface.java +++ b/consensus/ibftlegacy/src/main/java/org/hyperledger/besu/consensus/ibftlegacy/IbftLegacyBlockInterface.java @@ -15,8 +15,8 @@ package org.hyperledger.besu.consensus.ibftlegacy; import org.hyperledger.besu.consensus.common.BlockInterface; -import org.hyperledger.besu.consensus.common.ValidatorVote; -import org.hyperledger.besu.consensus.common.VoteType; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.ethereum.core.Address; import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.BlockHeaderBuilder; diff --git a/consensus/ibftlegacy/src/main/java/org/hyperledger/besu/consensus/ibftlegacy/headervalidationrules/IbftExtraDataValidationRule.java b/consensus/ibftlegacy/src/main/java/org/hyperledger/besu/consensus/ibftlegacy/headervalidationrules/IbftExtraDataValidationRule.java index eb0bdbb7d33..0480e0fbc06 100644 --- a/consensus/ibftlegacy/src/main/java/org/hyperledger/besu/consensus/ibftlegacy/headervalidationrules/IbftExtraDataValidationRule.java +++ b/consensus/ibftlegacy/src/main/java/org/hyperledger/besu/consensus/ibftlegacy/headervalidationrules/IbftExtraDataValidationRule.java @@ -14,7 +14,6 @@ */ package org.hyperledger.besu.consensus.ibftlegacy.headervalidationrules; -import org.hyperledger.besu.consensus.common.ValidatorProvider; import org.hyperledger.besu.consensus.common.bft.BftHelpers; import org.hyperledger.besu.consensus.ibft.IbftLegacyContext; import org.hyperledger.besu.consensus.ibftlegacy.IbftBlockHashing; @@ -56,17 +55,15 @@ public IbftExtraDataValidationRule(final boolean validateCommitSeals, final long public boolean validate( final BlockHeader header, final BlockHeader parent, final ProtocolContext context) { try { - final ValidatorProvider validatorProvider = + final Collection
storedValidators = context .getConsensusState(IbftLegacyContext.class) - .getVoteTallyCache() - .getVoteTallyAfterBlock(parent); + .getValidatorProvider() + .getValidatorsAfterBlock(parent); final IbftExtraData ibftExtraData = IbftExtraData.decode(header); final Address proposer = IbftBlockHashing.recoverProposerAddress(header, ibftExtraData); - final Collection
storedValidators = validatorProvider.getValidators(); - if (!storedValidators.contains(proposer)) { LOG.info("Invalid block header: Proposer sealing block is not a member of the validators."); return false; diff --git a/consensus/ibftlegacy/src/test/java/org/hyperledger/besu/consensus/ibftlegacy/IbftBlockHeaderValidationRulesetFactoryTest.java b/consensus/ibftlegacy/src/test/java/org/hyperledger/besu/consensus/ibftlegacy/IbftBlockHeaderValidationRulesetFactoryTest.java index 7d0da4507ad..a48fb29ca30 100644 --- a/consensus/ibftlegacy/src/test/java/org/hyperledger/besu/consensus/ibftlegacy/IbftBlockHeaderValidationRulesetFactoryTest.java +++ b/consensus/ibftlegacy/src/test/java/org/hyperledger/besu/consensus/ibftlegacy/IbftBlockHeaderValidationRulesetFactoryTest.java @@ -21,8 +21,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.consensus.ibft.IbftLegacyContext; import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.SECPSignature; @@ -53,11 +52,9 @@ public class IbftBlockHeaderValidationRulesetFactoryTest { private ProtocolContext setupContextWithValidators(final Collection
validators) { final IbftLegacyContext bftContext = mock(IbftLegacyContext.class); - final VoteTallyCache mockCache = mock(VoteTallyCache.class); - final VoteTally mockVoteTally = mock(VoteTally.class); - when(bftContext.getVoteTallyCache()).thenReturn(mockCache); - when(mockCache.getVoteTallyAfterBlock(any())).thenReturn(mockVoteTally); - when(mockVoteTally.getValidators()).thenReturn(validators); + final ValidatorProvider mockValidatorProvider = mock(ValidatorProvider.class); + when(bftContext.getValidatorProvider()).thenReturn(mockValidatorProvider); + when(mockValidatorProvider.getValidatorsAfterBlock(any())).thenReturn(validators); return new ProtocolContext(null, null, bftContext); } diff --git a/consensus/ibftlegacy/src/test/java/org/hyperledger/besu/consensus/ibftlegacy/IbftLegacyBlockInterfaceTest.java b/consensus/ibftlegacy/src/test/java/org/hyperledger/besu/consensus/ibftlegacy/IbftLegacyBlockInterfaceTest.java index ece948263d1..3f40ccd6b90 100644 --- a/consensus/ibftlegacy/src/test/java/org/hyperledger/besu/consensus/ibftlegacy/IbftLegacyBlockInterfaceTest.java +++ b/consensus/ibftlegacy/src/test/java/org/hyperledger/besu/consensus/ibftlegacy/IbftLegacyBlockInterfaceTest.java @@ -16,10 +16,10 @@ import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; -import static org.hyperledger.besu.consensus.common.VoteType.ADD; -import static org.hyperledger.besu.consensus.common.VoteType.DROP; +import static org.hyperledger.besu.consensus.common.validator.VoteType.ADD; +import static org.hyperledger.besu.consensus.common.validator.VoteType.DROP; -import org.hyperledger.besu.consensus.common.ValidatorVote; +import org.hyperledger.besu.consensus.common.validator.ValidatorVote; import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; import org.hyperledger.besu.ethereum.core.Address; diff --git a/consensus/ibftlegacy/src/test/java/org/hyperledger/besu/consensus/ibftlegacy/IbftLegacyContextBuilder.java b/consensus/ibftlegacy/src/test/java/org/hyperledger/besu/consensus/ibftlegacy/IbftLegacyContextBuilder.java index fecbb4e80ab..0c511e10275 100644 --- a/consensus/ibftlegacy/src/test/java/org/hyperledger/besu/consensus/ibftlegacy/IbftLegacyContextBuilder.java +++ b/consensus/ibftlegacy/src/test/java/org/hyperledger/besu/consensus/ibftlegacy/IbftLegacyContextBuilder.java @@ -19,9 +19,7 @@ import static org.mockito.Mockito.when; import static org.mockito.Mockito.withSettings; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.consensus.ibft.IbftLegacyContext; import org.hyperledger.besu.ethereum.core.Address; @@ -31,12 +29,10 @@ public class IbftLegacyContextBuilder { public static IbftLegacyContext setupContextWithValidators(final Collection
validators) { final IbftLegacyContext bftContext = mock(IbftLegacyContext.class, withSettings().lenient()); - final VoteTallyCache mockCache = mock(VoteTallyCache.class, withSettings().lenient()); - final VoteTally mockVoteTally = mock(VoteTally.class, withSettings().lenient()); - when(bftContext.getVoteTallyCache()).thenReturn(mockCache); - when(mockCache.getVoteTallyAfterBlock(any())).thenReturn(mockVoteTally); - when(mockVoteTally.getValidators()).thenReturn(validators); - when(bftContext.getVoteProposer()).thenReturn(new VoteProposer()); + final ValidatorProvider mockValidatorProvider = + mock(ValidatorProvider.class, withSettings().lenient()); + when(bftContext.getValidatorProvider()).thenReturn(mockValidatorProvider); + when(mockValidatorProvider.getValidatorsAfterBlock(any())).thenReturn(validators); return bftContext; } } diff --git a/consensus/qbft/src/integration-test/java/org/hyperledger/besu/consensus/qbft/support/TestContextBuilder.java b/consensus/qbft/src/integration-test/java/org/hyperledger/besu/consensus/qbft/support/TestContextBuilder.java index 03de86feb19..f782ab49566 100644 --- a/consensus/qbft/src/integration-test/java/org/hyperledger/besu/consensus/qbft/support/TestContextBuilder.java +++ b/consensus/qbft/src/integration-test/java/org/hyperledger/besu/consensus/qbft/support/TestContextBuilder.java @@ -21,9 +21,6 @@ import org.hyperledger.besu.config.StubGenesisConfigOptions; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTallyCache; -import org.hyperledger.besu.consensus.common.VoteTallyUpdater; import org.hyperledger.besu.consensus.common.bft.BftBlockHeaderFunctions; import org.hyperledger.besu.consensus.common.bft.BftBlockInterface; import org.hyperledger.besu.consensus.common.bft.BftContext; @@ -50,6 +47,8 @@ import org.hyperledger.besu.consensus.common.bft.statemachine.BftEventHandler; import org.hyperledger.besu.consensus.common.bft.statemachine.BftFinalState; import org.hyperledger.besu.consensus.common.bft.statemachine.FutureMessageBuffer; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider; import org.hyperledger.besu.consensus.qbft.QbftBlockHeaderValidationRulesetFactory; import org.hyperledger.besu.consensus.qbft.QbftExtraDataCodec; import org.hyperledger.besu.consensus.qbft.QbftGossip; @@ -306,20 +305,15 @@ private static ControllerAndState createControllerAndFinalState( final BftBlockInterface blockInterface = new BftBlockInterface(BFT_EXTRA_DATA_ENCODER); - final VoteTallyCache voteTallyCache = - new VoteTallyCache( - blockChain, - new VoteTallyUpdater(epochManager, blockInterface), - epochManager, - new BftBlockInterface(BFT_EXTRA_DATA_ENCODER)); - - final VoteProposer voteProposer = new VoteProposer(); + final ValidatorProvider validatorProvider = + BlockValidatorProvider.nonForkingValidatorProvider( + blockChain, epochManager, blockInterface); final ProtocolContext protocolContext = new ProtocolContext( blockChain, worldStateArchive, - new BftContext(voteTallyCache, voteProposer, epochManager, blockInterface)); + new BftContext(validatorProvider, epochManager, blockInterface)); final PendingTransactions pendingTransactions = new PendingTransactions( @@ -344,12 +338,12 @@ private static ControllerAndState createControllerAndFinalState( BFT_EXTRA_DATA_ENCODER); final ProposerSelector proposerSelector = - new ProposerSelector(blockChain, blockInterface, true, voteTallyCache); + new ProposerSelector(blockChain, blockInterface, true, validatorProvider); final BftExecutors bftExecutors = BftExecutors.create(new NoOpMetricsSystem()); final BftFinalState finalState = new BftFinalState( - protocolContext.getConsensusState(BftContext.class).getVoteTallyCache(), + protocolContext.getConsensusState(BftContext.class).getValidatorProvider(), nodeKey, Util.publicKeyToAddress(nodeKey.getPublicKey()), proposerSelector, diff --git a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftExtraDataCodec.java b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftExtraDataCodec.java index f5ba16a9f9e..8752c303e22 100644 --- a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftExtraDataCodec.java +++ b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/QbftExtraDataCodec.java @@ -17,10 +17,10 @@ import static org.hyperledger.besu.consensus.common.bft.Vote.ADD_BYTE_VALUE; import static org.hyperledger.besu.consensus.common.bft.Vote.DROP_BYTE_VALUE; -import org.hyperledger.besu.consensus.common.VoteType; import org.hyperledger.besu.consensus.common.bft.BftExtraData; import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec; import org.hyperledger.besu.consensus.common.bft.Vote; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.crypto.SECPSignature; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; import org.hyperledger.besu.ethereum.core.Address; diff --git a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/QbftJsonRpcMethods.java b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/QbftJsonRpcMethods.java index 8ee8b8f01ca..a1cc21693a0 100644 --- a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/QbftJsonRpcMethods.java +++ b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/QbftJsonRpcMethods.java @@ -16,11 +16,10 @@ import org.hyperledger.besu.consensus.common.BlockInterface; import org.hyperledger.besu.consensus.common.EpochManager; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteTallyCache; -import org.hyperledger.besu.consensus.common.VoteTallyUpdater; import org.hyperledger.besu.consensus.common.bft.BftBlockInterface; import org.hyperledger.besu.consensus.common.bft.BftContext; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider; import org.hyperledger.besu.consensus.qbft.jsonrpc.methods.QbftDiscardValidatorVote; import org.hyperledger.besu.consensus.qbft.jsonrpc.methods.QbftGetPendingVotes; import org.hyperledger.besu.consensus.qbft.jsonrpc.methods.QbftGetSignerMetrics; @@ -55,26 +54,28 @@ protected Map create() { final BlockchainQueries blockchainQueries = new BlockchainQueries(context.getBlockchain(), context.getWorldStateArchive()); final BftContext bftContext = context.getConsensusState(BftContext.class); - final VoteProposer voteProposer = bftContext.getVoteProposer(); final BlockInterface blockInterface = bftContext.getBlockInterface(); + final ValidatorProvider validatorProvider = bftContext.getValidatorProvider(); - final VoteTallyCache voteTallyCache = createVoteTallyCache(context, mutableBlockchain); + // Must create our own voteTallyCache as using this would pollute the main voteTallyCache + final ValidatorProvider readOnlyValidatorProvider = + createValidatorProvider(context, mutableBlockchain); return mapOf( - new QbftProposeValidatorVote(voteProposer), + new QbftProposeValidatorVote(validatorProvider), new QbftGetValidatorsByBlockNumber(blockchainQueries, blockInterface), - new QbftDiscardValidatorVote(voteProposer), + new QbftDiscardValidatorVote(validatorProvider), new QbftGetValidatorsByBlockHash(context.getBlockchain(), blockInterface), - new QbftGetSignerMetrics(voteTallyCache, blockInterface, blockchainQueries), - new QbftGetPendingVotes(voteProposer)); + new QbftGetSignerMetrics(readOnlyValidatorProvider, blockInterface, blockchainQueries), + new QbftGetPendingVotes(validatorProvider)); } - private VoteTallyCache createVoteTallyCache( + private ValidatorProvider createValidatorProvider( final ProtocolContext context, final MutableBlockchain blockchain) { final BftContext bftContext = context.getConsensusState(BftContext.class); final EpochManager epochManager = bftContext.getEpochManager(); final BftBlockInterface bftBlockInterface = bftContext.getBlockInterface(); - final VoteTallyUpdater voteTallyUpdater = new VoteTallyUpdater(epochManager, bftBlockInterface); - return new VoteTallyCache(blockchain, voteTallyUpdater, epochManager, bftBlockInterface); + return BlockValidatorProvider.nonForkingValidatorProvider( + blockchain, epochManager, bftBlockInterface); } } diff --git a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftDiscardValidatorVote.java b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftDiscardValidatorVote.java index d4da3248d35..d50b972463c 100644 --- a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftDiscardValidatorVote.java +++ b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftDiscardValidatorVote.java @@ -14,10 +14,12 @@ */ package org.hyperledger.besu.consensus.qbft.jsonrpc.methods; -import org.hyperledger.besu.consensus.common.VoteProposer; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; 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.core.Address; @@ -27,10 +29,10 @@ public class QbftDiscardValidatorVote implements JsonRpcMethod { private static final Logger LOG = LogManager.getLogger(); - private final VoteProposer voteProposer; + private final ValidatorProvider validatorProvider; - public QbftDiscardValidatorVote(final VoteProposer voteProposer) { - this.voteProposer = voteProposer; + public QbftDiscardValidatorVote(final ValidatorProvider validatorProvider) { + this.validatorProvider = validatorProvider; } @Override @@ -40,10 +42,15 @@ public String getName() { @Override public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { - final Address validatorAddress = requestContext.getRequiredParameter(0, Address.class); - LOG.trace("Received RPC rpcName={} address={}", getName(), validatorAddress); - voteProposer.discard(validatorAddress); + if (validatorProvider.getVoteProvider().isPresent()) { + final Address validatorAddress = requestContext.getRequiredParameter(0, Address.class); + LOG.trace("Received RPC rpcName={} address={}", getName(), validatorAddress); + validatorProvider.getVoteProvider().get().discardVote(validatorAddress); - return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), true); + return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), true); + } else { + return new JsonRpcErrorResponse( + requestContext.getRequest().getId(), JsonRpcError.METHOD_UNIMPLEMENTED); + } } } diff --git a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetPendingVotes.java b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetPendingVotes.java index f4fb098d57a..b5c7af03674 100644 --- a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetPendingVotes.java +++ b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetPendingVotes.java @@ -14,15 +14,15 @@ */ package org.hyperledger.besu.consensus.qbft.jsonrpc.methods; -import org.hyperledger.besu.consensus.common.VoteProposer; import org.hyperledger.besu.consensus.common.jsonrpc.AbstractVoteProposerMethod; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; public class QbftGetPendingVotes extends AbstractVoteProposerMethod implements JsonRpcMethod { - public QbftGetPendingVotes(final VoteProposer voteProposer) { - super(voteProposer); + public QbftGetPendingVotes(final ValidatorProvider validatorProvider) { + super(validatorProvider); } @Override diff --git a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetSignerMetrics.java b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetSignerMetrics.java index 212eda12174..d506abc1b6b 100644 --- a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetSignerMetrics.java +++ b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetSignerMetrics.java @@ -15,8 +15,8 @@ package org.hyperledger.besu.consensus.qbft.jsonrpc.methods; import org.hyperledger.besu.consensus.common.BlockInterface; -import org.hyperledger.besu.consensus.common.VoteTallyCache; import org.hyperledger.besu.consensus.common.jsonrpc.AbstractGetSignerMetricsMethod; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; @@ -24,10 +24,10 @@ public class QbftGetSignerMetrics extends AbstractGetSignerMetricsMethod implements JsonRpcMethod { public QbftGetSignerMetrics( - final VoteTallyCache voteTallyCache, + final ValidatorProvider validatorProvider, final BlockInterface blockInterface, final BlockchainQueries blockchainQueries) { - super(voteTallyCache, blockInterface, blockchainQueries); + super(validatorProvider, blockInterface, blockchainQueries); } @Override diff --git a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftProposeValidatorVote.java b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftProposeValidatorVote.java index 7aa4febd745..7d6513d893e 100644 --- a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftProposeValidatorVote.java +++ b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftProposeValidatorVote.java @@ -14,11 +14,13 @@ */ package org.hyperledger.besu.consensus.qbft.jsonrpc.methods; -import org.hyperledger.besu.consensus.common.VoteProposer; -import org.hyperledger.besu.consensus.common.VoteType; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; 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.core.Address; @@ -28,10 +30,10 @@ public class QbftProposeValidatorVote implements JsonRpcMethod { private static final Logger LOG = LogManager.getLogger(); - private final VoteProposer voteProposer; + private final ValidatorProvider validatorProvider; - public QbftProposeValidatorVote(final VoteProposer voteProposer) { - this.voteProposer = voteProposer; + public QbftProposeValidatorVote(final ValidatorProvider validatorProvider) { + this.validatorProvider = validatorProvider; } @Override @@ -41,21 +43,24 @@ public String getName() { @Override public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { + if (validatorProvider.getVoteProvider().isPresent()) { + final Address validatorAddress = requestContext.getRequiredParameter(0, Address.class); + final Boolean add = requestContext.getRequiredParameter(1, Boolean.class); + LOG.trace( + "Received RPC rpcName={} voteType={} address={}", + getName(), + add ? VoteType.ADD : VoteType.DROP, + validatorAddress); - final Address validatorAddress = requestContext.getRequiredParameter(0, Address.class); - final Boolean add = requestContext.getRequiredParameter(1, Boolean.class); - LOG.trace( - "Received RPC rpcName={} voteType={} address={}", - getName(), - add ? VoteType.ADD : VoteType.DROP, - validatorAddress); - - if (add) { - voteProposer.auth(validatorAddress); + if (add) { + validatorProvider.getVoteProvider().get().authVote(validatorAddress); + } else { + validatorProvider.getVoteProvider().get().dropVote(validatorAddress); + } + return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), true); } else { - voteProposer.drop(validatorAddress); + return new JsonRpcErrorResponse( + requestContext.getRequest().getId(), JsonRpcError.METHOD_UNIMPLEMENTED); } - - return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), true); } } diff --git a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/validation/MessageValidatorFactory.java b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/validation/MessageValidatorFactory.java index bd1070ab668..50ade9fd457 100644 --- a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/validation/MessageValidatorFactory.java +++ b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/validation/MessageValidatorFactory.java @@ -50,9 +50,8 @@ public MessageValidatorFactory( private Collection
getValidatorsAfterBlock(final BlockHeader parentHeader) { return protocolContext .getConsensusState(BftContext.class) - .getVoteTallyCache() - .getVoteTallyAfterBlock(parentHeader) - .getValidators(); + .getValidatorProvider() + .getValidatorsAfterBlock(parentHeader); } public RoundChangeMessageValidator createRoundChangeMessageValidator( diff --git a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftExtraDataCodecTest.java b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftExtraDataCodecTest.java index ea7c7305c8f..1f49eff62fb 100644 --- a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftExtraDataCodecTest.java +++ b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftExtraDataCodecTest.java @@ -17,9 +17,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import org.hyperledger.besu.consensus.common.VoteType; import org.hyperledger.besu.consensus.common.bft.BftExtraData; import org.hyperledger.besu.consensus.common.bft.Vote; +import org.hyperledger.besu.consensus.common.validator.VoteType; import org.hyperledger.besu.crypto.SECPSignature; import org.hyperledger.besu.crypto.SignatureAlgorithm; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; diff --git a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftDiscardValidatorVoteTest.java b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftDiscardValidatorVoteTest.java index c1cb8d7bd09..402fc1b112e 100644 --- a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftDiscardValidatorVoteTest.java +++ b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftDiscardValidatorVoteTest.java @@ -17,8 +17,10 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -import org.hyperledger.besu.consensus.common.VoteProposer; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.VoteProvider; 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.exception.InvalidJsonRpcParameters; @@ -26,13 +28,16 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.core.Address; +import java.util.Optional; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; public class QbftDiscardValidatorVoteTest { - private final VoteProposer voteProposer = mock(VoteProposer.class); + private final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + private final VoteProvider voteProvider = mock(VoteProvider.class); private final String QBFT_METHOD = "qbft_discardValidatorVote"; private final String JSON_RPC_VERSION = "2.0"; private QbftDiscardValidatorVote method; @@ -41,7 +46,8 @@ public class QbftDiscardValidatorVoteTest { @Before public void setup() { - method = new QbftDiscardValidatorVote(voteProposer); + method = new QbftDiscardValidatorVote(validatorProvider); + when(validatorProvider.getVoteProvider()).thenReturn(Optional.of(voteProvider)); } @Test @@ -81,7 +87,7 @@ public void discardValidator() { assertThat(response).isEqualToComparingFieldByField(expectedResponse); - verify(voteProposer).discard(parameterAddress); + verify(voteProvider).discardVote(parameterAddress); } private JsonRpcRequestContext requestWithParams(final Object... params) { diff --git a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetPendingVotesTest.java b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetPendingVotesTest.java index e69292c33d4..aaf4ee38216 100644 --- a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetPendingVotesTest.java +++ b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetPendingVotesTest.java @@ -38,7 +38,7 @@ protected String getMethodName() { @Before public void setup() { - method = new QbftGetPendingVotes(getVoteProposer()); + method = new QbftGetPendingVotes(getValidatorProvider()); } @Test diff --git a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetSignerMetricsTest.java b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetSignerMetricsTest.java index d4621140f76..fe161510504 100644 --- a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetSignerMetricsTest.java +++ b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetSignerMetricsTest.java @@ -22,8 +22,7 @@ import static org.mockito.Mockito.when; import org.hyperledger.besu.consensus.common.BlockInterface; -import org.hyperledger.besu.consensus.common.VoteTally; -import org.hyperledger.besu.consensus.common.VoteTallyCache; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; 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.exception.InvalidJsonRpcParameters; @@ -56,7 +55,7 @@ public class QbftGetSignerMetricsTest { private final String JSON_RPC_VERSION = "2.0"; private QbftGetSignerMetrics method; - private VoteTallyCache voteTallyCache; + private ValidatorProvider validatorProvider; private BlockchainQueries blockchainQueries; private BlockInterface blockInterface; @@ -64,10 +63,10 @@ public class QbftGetSignerMetricsTest { @Before public void setup() { - voteTallyCache = mock(VoteTallyCache.class); + validatorProvider = mock(ValidatorProvider.class); blockchainQueries = mock(BlockchainQueries.class); blockInterface = mock(BlockInterface.class); - method = new QbftGetSignerMetrics(voteTallyCache, blockInterface, blockchainQueries); + method = new QbftGetSignerMetrics(validatorProvider, blockInterface, blockchainQueries); } @Test @@ -259,8 +258,7 @@ private SignerMetricResult generateBlock(final long number) { when(blockchainQueries.getBlockHeaderByNumber(number)).thenReturn(Optional.of(header)); when(blockInterface.getProposerOfBlock(header)).thenReturn(proposerAddressBlock); - when(voteTallyCache.getVoteTallyAfterBlock(header)) - .thenReturn(new VoteTally(Arrays.asList(VALIDATORS))); + when(validatorProvider.getValidatorsAfterBlock(header)).thenReturn(Arrays.asList(VALIDATORS)); final SignerMetricResult signerMetricResult = new SignerMetricResult(proposerAddressBlock); signerMetricResult.incrementeNbBlock(); diff --git a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftProposeValidatorVoteTest.java b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftProposeValidatorVoteTest.java index ce139ec6be1..867ff736917 100644 --- a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftProposeValidatorVoteTest.java +++ b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftProposeValidatorVoteTest.java @@ -17,8 +17,10 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -import org.hyperledger.besu.consensus.common.VoteProposer; +import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; +import org.hyperledger.besu.consensus.common.validator.VoteProvider; 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.exception.InvalidJsonRpcParameters; @@ -26,13 +28,16 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.core.Address; +import java.util.Optional; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; public class QbftProposeValidatorVoteTest { - private final VoteProposer voteProposer = mock(VoteProposer.class); + private final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); + private final VoteProvider voteProvider = mock(VoteProvider.class); private final String QBFT_METHOD = "qbft_proposeValidatorVote"; private final String JSON_RPC_VERSION = "2.0"; private QbftProposeValidatorVote method; @@ -41,7 +46,8 @@ public class QbftProposeValidatorVoteTest { @Before public void setup() { - method = new QbftProposeValidatorVote(voteProposer); + method = new QbftProposeValidatorVote(validatorProvider); + when(validatorProvider.getVoteProvider()).thenReturn(Optional.of(voteProvider)); } @Test @@ -100,7 +106,7 @@ public void addValidator() { assertThat(response).isEqualToComparingFieldByField(expectedResponse); - verify(voteProposer).auth(parameterAddress); + verify(voteProvider).authVote(parameterAddress); } @Test @@ -114,7 +120,7 @@ public void removeValidator() { assertThat(response).isEqualToComparingFieldByField(expectedResponse); - verify(voteProposer).drop(parameterAddress); + verify(voteProvider).dropVote(parameterAddress); } private JsonRpcRequestContext requestWithParams(final Object... params) {