Skip to content

Commit

Permalink
Added abstract BftBesuControllerBuilder (hyperledger#2573)
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Saldanha <lucascrsaldanha@gmail.com>
  • Loading branch information
lucassaldanha authored Jul 25, 2021
1 parent b81b234 commit 729027f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.controller;

import org.hyperledger.besu.consensus.common.bft.BftBlockInterface;
import org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec;

import java.util.function.Supplier;

import com.google.common.base.Suppliers;

public abstract class BftBesuControllerBuilder extends BesuControllerBuilder {

protected abstract Supplier<BftExtraDataCodec> bftExtraDataCodec();

protected Supplier<BftBlockInterface> bftBlockInterface() {
return Suppliers.memoize(() -> new BftBlockInterface(bftExtraDataCodec().get()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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.bft.BftEventQueue;
import org.hyperledger.besu.consensus.common.bft.BftExecutors;
Expand Down Expand Up @@ -76,19 +75,24 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import com.google.common.base.Suppliers;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class IbftBesuControllerBuilder extends BesuControllerBuilder {
public class IbftBesuControllerBuilder extends BftBesuControllerBuilder {

private static final Logger LOG = LogManager.getLogger();
private BftEventQueue bftEventQueue;
private BftConfigOptions bftConfig;
private ValidatorPeers peers;
private final BftExtraDataCodec bftExtraDataCodec = new IbftExtraDataCodec();
private final BftBlockInterface blockInterface = new BftBlockInterface(bftExtraDataCodec);

@Override
protected Supplier<BftExtraDataCodec> bftExtraDataCodec() {
return Suppliers.memoize(IbftExtraDataCodec::new);
}

@Override
protected void prepForBuild() {
Expand Down Expand Up @@ -134,15 +138,15 @@ protected MiningCoordinator createMiningCoordinator(
miningParameters,
localAddress,
bftConfig.getMiningBeneficiary().map(Address::fromHexString).orElse(localAddress),
bftExtraDataCodec);
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 ProposerSelector proposerSelector =
new ProposerSelector(blockchain, blockInterface, true, voteTallyCache);
new ProposerSelector(blockchain, bftBlockInterface().get(), true, voteTallyCache);

peers = new ValidatorPeers(voteTallyCache, IbftSubProtocol.NAME);

Expand All @@ -165,7 +169,7 @@ protected MiningCoordinator createMiningCoordinator(

final MessageValidatorFactory messageValidatorFactory =
new MessageValidatorFactory(
proposerSelector, protocolSchedule, protocolContext, bftExtraDataCodec);
proposerSelector, protocolSchedule, protocolContext, bftExtraDataCodec().get());

final Subscribers<MinedBlockObserver> minedBlockObservers = Subscribers.create();
minedBlockObservers.subscribe(ethProtocolManager);
Expand Down Expand Up @@ -194,7 +198,7 @@ protected MiningCoordinator createMiningCoordinator(
minedBlockObservers,
messageValidatorFactory,
messageFactory,
bftExtraDataCodec),
bftExtraDataCodec().get()),
messageValidatorFactory,
messageFactory),
gossiper,
Expand All @@ -220,7 +224,7 @@ protected MiningCoordinator createMiningCoordinator(

@Override
protected PluginServiceFactory createAdditionalPluginServices(final Blockchain blockchain) {
return new IbftQueryPluginServiceFactory(blockchain, blockInterface, nodeKey);
return new IbftQueryPluginServiceFactory(blockchain, bftBlockInterface().get(), nodeKey);
}

@Override
Expand All @@ -230,14 +234,14 @@ protected ProtocolSchedule createProtocolSchedule() {
privacyParameters,
isRevertReasonEnabled,
IbftBlockHeaderValidationRulesetFactory::blockHeaderValidator,
bftExtraDataCodec);
bftExtraDataCodec().get());
}

@Override
protected void validateContext(final ProtocolContext context) {
final BlockHeader genesisBlockHeader = context.getBlockchain().getGenesisBlock().getHeader();

if (blockInterface.validatorsInBlock(genesisBlockHeader).isEmpty()) {
if (bftBlockInterface().get().validatorsInBlock(genesisBlockHeader).isEmpty()) {
LOG.warn("Genesis block contains no signers - chain will not progress.");
}
}
Expand All @@ -256,13 +260,13 @@ protected BftContext createConsensusContext(
return new BftContext(
new ForkingVoteTallyCache(
blockchain,
new VoteTallyUpdater(epochManager, blockInterface),
new VoteTallyUpdater(epochManager, bftBlockInterface().get()),
epochManager,
blockInterface,
bftBlockInterface().get(),
new BftValidatorOverrides(ibftValidatorForkMap)),
new VoteProposer(),
epochManager,
blockInterface);
bftBlockInterface().get());
}

private Map<Long, List<Address>> convertIbftForks(final List<BftFork> bftForks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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.bft.BftEventQueue;
import org.hyperledger.besu.consensus.common.bft.BftExecutors;
Expand Down Expand Up @@ -76,19 +75,24 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import com.google.common.base.Suppliers;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class QbftBesuControllerBuilder extends BesuControllerBuilder {
public class QbftBesuControllerBuilder extends BftBesuControllerBuilder {

private static final Logger LOG = LogManager.getLogger();
private BftEventQueue bftEventQueue;
private BftConfigOptions bftConfig;
private ValidatorPeers peers;
private final BftExtraDataCodec bftExtraDataCodec = new QbftExtraDataCodec();
private final BftBlockInterface blockInterface = new BftBlockInterface(bftExtraDataCodec);

@Override
protected Supplier<BftExtraDataCodec> bftExtraDataCodec() {
return Suppliers.memoize(QbftExtraDataCodec::new);
}

@Override
protected void prepForBuild() {
Expand Down Expand Up @@ -137,13 +141,13 @@ protected MiningCoordinator createMiningCoordinator(
miningParameters,
localAddress,
bftConfig.getMiningBeneficiary().map(Address::fromHexString).orElse(localAddress),
bftExtraDataCodec);
bftExtraDataCodec().get());

final VoteTallyCache voteTallyCache =
protocolContext.getConsensusState(BftContext.class).getVoteTallyCache();

final ProposerSelector proposerSelector =
new ProposerSelector(blockchain, blockInterface, true, voteTallyCache);
new ProposerSelector(blockchain, bftBlockInterface().get(), true, voteTallyCache);

// NOTE: peers should not be used for accessing the network as it does not enforce the
// "only send once" filter applied by the UniqueMessageMulticaster.
Expand All @@ -168,7 +172,7 @@ protected MiningCoordinator createMiningCoordinator(

final MessageValidatorFactory messageValidatorFactory =
new MessageValidatorFactory(
proposerSelector, protocolSchedule, protocolContext, bftExtraDataCodec);
proposerSelector, protocolSchedule, protocolContext, bftExtraDataCodec().get());

final Subscribers<MinedBlockObserver> minedBlockObservers = Subscribers.create();
minedBlockObservers.subscribe(ethProtocolManager);
Expand Down Expand Up @@ -197,7 +201,7 @@ protected MiningCoordinator createMiningCoordinator(
minedBlockObservers,
messageValidatorFactory,
messageFactory,
bftExtraDataCodec),
bftExtraDataCodec().get()),
messageValidatorFactory,
messageFactory),
gossiper,
Expand All @@ -223,7 +227,7 @@ protected MiningCoordinator createMiningCoordinator(

@Override
protected PluginServiceFactory createAdditionalPluginServices(final Blockchain blockchain) {
return new BftQueryPluginServiceFactory(blockchain, bftExtraDataCodec, nodeKey, "qbft");
return new BftQueryPluginServiceFactory(blockchain, bftExtraDataCodec().get(), nodeKey, "qbft");
}

@Override
Expand All @@ -233,14 +237,14 @@ protected ProtocolSchedule createProtocolSchedule() {
privacyParameters,
isRevertReasonEnabled,
QbftBlockHeaderValidationRulesetFactory::blockHeaderValidator,
bftExtraDataCodec);
bftExtraDataCodec().get());
}

@Override
protected void validateContext(final ProtocolContext context) {
final BlockHeader genesisBlockHeader = context.getBlockchain().getGenesisBlock().getHeader();

if (blockInterface.validatorsInBlock(genesisBlockHeader).isEmpty()) {
if (bftBlockInterface().get().validatorsInBlock(genesisBlockHeader).isEmpty()) {
LOG.warn("Genesis block contains no signers - chain will not progress.");
}
}
Expand All @@ -259,13 +263,13 @@ protected BftContext createConsensusContext(
return new BftContext(
new ForkingVoteTallyCache(
blockchain,
new VoteTallyUpdater(epochManager, blockInterface),
new VoteTallyUpdater(epochManager, bftBlockInterface().get()),
epochManager,
blockInterface,
bftBlockInterface().get(),
new BftValidatorOverrides(bftValidatorForkMap)),
new VoteProposer(),
epochManager,
blockInterface);
bftBlockInterface().get());
}

private Map<Long, List<Address>> convertBftForks(final List<BftFork> bftForks) {
Expand Down

0 comments on commit 729027f

Please sign in to comment.