Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ibft queries #138

Merged
merged 18 commits into from
Oct 31, 2019
20 changes: 2 additions & 18 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@
import org.hyperledger.besu.cli.util.ConfigOptionSearchAndRunHandler;
import org.hyperledger.besu.cli.util.VersionProvider;
import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.consensus.common.PoAContext;
import org.hyperledger.besu.consensus.common.PoAMetricServiceImpl;
import org.hyperledger.besu.controller.BesuController;
import org.hyperledger.besu.controller.BesuControllerBuilder;
import org.hyperledger.besu.controller.KeyPairUtil;
Expand Down Expand Up @@ -105,7 +103,6 @@
import org.hyperledger.besu.plugin.services.exception.StorageException;
import org.hyperledger.besu.plugin.services.metrics.MetricCategory;
import org.hyperledger.besu.plugin.services.metrics.MetricCategoryRegistry;
import org.hyperledger.besu.plugin.services.metrics.PoAMetricsService;
import org.hyperledger.besu.plugin.services.storage.rocksdb.RocksDBPlugin;
import org.hyperledger.besu.services.BesuConfigurationImpl;
import org.hyperledger.besu.services.BesuEventsImpl;
Expand Down Expand Up @@ -927,25 +924,12 @@ private BesuCommand startPlugins() {
besuController.getProtocolManager().getBlockBroadcaster(),
besuController.getTransactionPool(),
besuController.getSyncState()));
addPluginMetrics(besuController);
besuPluginContext.addService(MetricsSystem.class, getMetricsSystem());
besuController.getAdditionalPluginServices().appendPluginServices(besuPluginContext);
besuPluginContext.startPlugins();
return this;
}

private void addPluginMetrics(final BesuController<?> besuController) {
besuPluginContext.addService(MetricsSystem.class, getMetricsSystem());

final Object consensusState = besuController.getProtocolContext().getConsensusState();

if (consensusState != null && PoAContext.class.isAssignableFrom(consensusState.getClass())) {
final PoAMetricServiceImpl service =
new PoAMetricServiceImpl(
((PoAContext) consensusState).getBlockInterface(),
besuController.getProtocolContext().getBlockchain());
besuPluginContext.addService(PoAMetricsService.class, service);
}
}

private void prepareLogging() {
// set log level per CLI flags
if (logLevel != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class BesuController<C> implements java.io.Closeable {
private final MiningCoordinator miningCoordinator;
private final PrivacyParameters privacyParameters;
private final List<Closeable> closeables;
private final PluginServiceFactory additionalPluginServices;
private final SyncState syncState;

BesuController(
Expand All @@ -73,7 +74,8 @@ public class BesuController<C> implements java.io.Closeable {
final PrivacyParameters privacyParameters,
final JsonRpcMethodFactory additionalJsonRpcMethodsFactory,
final KeyPair keyPair,
final List<Closeable> closeables) {
final List<Closeable> closeables,
final PluginServiceFactory additionalPluginServices) {
this.protocolSchedule = protocolSchedule;
this.protocolContext = protocolContext;
this.ethProtocolManager = ethProtocolManager;
Expand All @@ -87,6 +89,7 @@ public class BesuController<C> implements java.io.Closeable {
this.miningCoordinator = miningCoordinator;
this.privacyParameters = privacyParameters;
this.closeables = closeables;
this.additionalPluginServices = additionalPluginServices;
}

public ProtocolContext<C> getProtocolContext() {
Expand Down Expand Up @@ -151,6 +154,10 @@ public SyncState getSyncState() {
return syncState;
}

public PluginServiceFactory getAdditionalPluginServices() {
return additionalPluginServices;
}

public static class Builder {

public BesuControllerBuilder<?> fromEthNetworkConfig(final EthNetworkConfig ethNetworkConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ public BesuController<C> build() {
syncState,
ethProtocolManager);

final PluginServiceFactory additionalPluginServices =
createAdditionalPluginServices(blockchain);

final SubProtocolConfiguration subProtocolConfiguration =
createSubProtocolConfiguration(ethProtocolManager);

Expand All @@ -298,7 +301,8 @@ public BesuController<C> build() {
privacyParameters,
additionalJsonRpcMethodFactory,
nodeKeys,
closeables);
closeables,
additionalPluginServices);
}

protected void prepForBuild() {}
Expand Down Expand Up @@ -365,4 +369,7 @@ private List<PeerValidator> createPeerValidators(final ProtocolSchedule<C> proto

return validators;
}

protected abstract PluginServiceFactory createAdditionalPluginServices(
final Blockchain blockchain);
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ protected void validateContext(final ProtocolContext<CliqueContext> context) {
}
}

@Override
protected PluginServiceFactory createAdditionalPluginServices(final Blockchain blockchain) {
return new CliqueQueryPluginServiceFactory(blockchain);
}

@Override
protected CliqueContext createConsensusContext(
final Blockchain blockchain, final WorldStateArchive worldStateArchive) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.clique.CliqueBlockInterface;
import org.hyperledger.besu.consensus.common.BlockInterface;
import org.hyperledger.besu.consensus.common.PoaQueryServiceImpl;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.plugin.services.metrics.PoAMetricsService;
import org.hyperledger.besu.plugin.services.query.PoaQueryService;
import org.hyperledger.besu.services.BesuPluginContextImpl;

public class CliqueQueryPluginServiceFactory implements PluginServiceFactory {

final Blockchain blockchain;

public CliqueQueryPluginServiceFactory(final Blockchain blockchain) {
this.blockchain = blockchain;
}

@Override
public void appendPluginServices(final BesuPluginContextImpl besuContext) {
final BlockInterface blockInterface = new CliqueBlockInterface();

final PoaQueryServiceImpl service = new PoaQueryServiceImpl(blockInterface, blockchain);
besuContext.addService(PoaQueryService.class, service);
besuContext.addService(PoAMetricsService.class, service);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ protected MiningCoordinator createMiningCoordinator(
return ibftMiningCoordinator;
}

@Override
protected PluginServiceFactory createAdditionalPluginServices(final Blockchain blockchain) {
return new IbftQueryPluginServiceFactory(blockchain);
}

@Override
protected ProtocolSchedule<IbftContext> createProtocolSchedule() {
return IbftProtocolSchedule.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ protected IbftContext createConsensusContext(
return new IbftContext(voteTallyCache, voteProposer, blockInterface);
}

@Override
protected PluginServiceFactory createAdditionalPluginServices(final Blockchain blockchain) {
return new NoopPluginServiceFactory();
}

@Override
protected void validateContext(final ProtocolContext<IbftContext> context) {
final BlockHeader genesisBlockHeader = context.getBlockchain().getGenesisBlock().getHeader();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.BlockInterface;
import org.hyperledger.besu.consensus.ibft.IbftBlockInterface;
import org.hyperledger.besu.consensus.ibft.queries.IbftQueryServiceImpl;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.plugin.services.metrics.PoAMetricsService;
import org.hyperledger.besu.plugin.services.query.IbftQueryService;
import org.hyperledger.besu.plugin.services.query.PoaQueryService;
import org.hyperledger.besu.services.BesuPluginContextImpl;

public class IbftQueryPluginServiceFactory implements PluginServiceFactory {

final Blockchain blockchain;

public IbftQueryPluginServiceFactory(final Blockchain blockchain) {
this.blockchain = blockchain;
}

@Override
public void appendPluginServices(final BesuPluginContextImpl besuContext) {
final BlockInterface blockInterface = new IbftBlockInterface();

final IbftQueryServiceImpl service = new IbftQueryServiceImpl(blockInterface, blockchain);
besuContext.addService(IbftQueryService.class, service);
besuContext.addService(PoaQueryService.class, service);
besuContext.addService(PoAMetricsService.class, service);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ protected Void createConsensusContext(
return null;
}

@Override
protected PluginServiceFactory createAdditionalPluginServices(final Blockchain blockchain) {
return new NoopPluginServiceFactory();
}

@Override
protected ProtocolSchedule<Void> createProtocolSchedule() {
return MainnetProtocolSchedule.fromConfig(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.services.BesuPluginContextImpl;

public class NoopPluginServiceFactory implements PluginServiceFactory {

@Override
public void appendPluginServices(final BesuPluginContextImpl besuContext) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.services.BesuPluginContextImpl;

public interface PluginServiceFactory {

void appendPluginServices(BesuPluginContextImpl besuContext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.hyperledger.besu.cli.subcommands.blocks.BlocksSubCommand;
import org.hyperledger.besu.controller.BesuController;
import org.hyperledger.besu.controller.BesuControllerBuilder;
import org.hyperledger.besu.controller.NoopPluginServiceFactory;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.graphql.GraphQLConfiguration;
Expand Down Expand Up @@ -177,6 +178,9 @@ public void initMocks() throws Exception {
lenient().when(mockController.getProtocolManager()).thenReturn(mockEthProtocolManager);
lenient().when(mockController.getProtocolSchedule()).thenReturn(mockProtocolSchedule);
lenient().when(mockController.getProtocolContext()).thenReturn(mockProtocolContext);
lenient()
.when(mockController.getAdditionalPluginServices())
.thenReturn(new NoopPluginServiceFactory());

when(mockEthProtocolManager.getBlockBroadcaster()).thenReturn(mockBlockBroadcaster);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

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.PoaContext;
import org.hyperledger.besu.consensus.common.VoteProposer;
import org.hyperledger.besu.consensus.common.VoteTallyCache;

/**
* Holds the data which lives "in parallel" with the importation of blocks etc. when using the
* Clique consensus mechanism.
*/
public class CliqueContext implements PoAContext {
public class CliqueContext implements PoaContext {

private final VoteTallyCache voteTallyCache;
private final VoteProposer voteProposer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
*/
package org.hyperledger.besu.consensus.common;

public interface PoAContext {
public interface PoaContext {
BlockInterface getBlockInterface();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
import org.hyperledger.besu.plugin.data.Address;
import org.hyperledger.besu.plugin.data.BlockHeader;
import org.hyperledger.besu.plugin.services.metrics.PoAMetricsService;
import org.hyperledger.besu.plugin.services.query.PoaQueryService;

import java.util.ArrayList;
import java.util.Collection;

public class PoAMetricServiceImpl implements PoAMetricsService {
public class PoaQueryServiceImpl implements PoaQueryService, PoAMetricsService {

private final BlockInterface blockInterface;
private final Blockchain blockchain;

public PoAMetricServiceImpl(final BlockInterface blockInterface, final Blockchain blockchain) {
public PoaQueryServiceImpl(final BlockInterface blockInterface, final Blockchain blockchain) {
this.blockInterface = blockInterface;
this.blockchain = blockchain;
}
Expand All @@ -41,4 +42,8 @@ public Collection<Address> getValidatorsForLatestBlock() {
public Address getProposerOfBlock(final BlockHeader header) {
return this.blockInterface.getProposerOfBlock(header);
}

protected Blockchain getBlockchain() {
return blockchain;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package org.hyperledger.besu.consensus.ibft;

import org.hyperledger.besu.consensus.common.BlockInterface;
import org.hyperledger.besu.consensus.common.PoAContext;
import org.hyperledger.besu.consensus.common.PoaContext;
import org.hyperledger.besu.consensus.common.VoteProposer;
import org.hyperledger.besu.consensus.common.VoteTallyCache;

/** Holds the IBFT specific mutable state. */
public class IbftContext implements PoAContext {
public class IbftContext implements PoaContext {

private final VoteTallyCache voteTallyCache;
private final VoteProposer voteProposer;
Expand Down
Loading