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().appendQueries(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 @@ -51,6 +51,7 @@ public class BesuController<C> implements java.io.Closeable {
private final MiningCoordinator miningCoordinator;
private final PrivacyParameters privacyParameters;
private final Runnable close;
private final PluginServiceFactory additionalPluginServices;
private final SyncState syncState;

BesuController(
Expand All @@ -66,7 +67,8 @@ public class BesuController<C> implements java.io.Closeable {
final PrivacyParameters privacyParameters,
final Runnable close,
final JsonRpcMethodFactory additionalJsonRpcMethodsFactory,
final KeyPair keyPair) {
final KeyPair keyPair,
final PluginServiceFactory additionalPluginServices) {
this.protocolSchedule = protocolSchedule;
this.protocolContext = protocolContext;
this.ethProtocolManager = ethProtocolManager;
Expand All @@ -80,6 +82,7 @@ public class BesuController<C> implements java.io.Closeable {
this.miningCoordinator = miningCoordinator;
this.privacyParameters = privacyParameters;
this.close = close;
this.additionalPluginServices = additionalPluginServices;
}

public ProtocolContext<C> getProtocolContext() {
Expand Down Expand Up @@ -136,6 +139,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 @@ -299,11 +299,15 @@ public BesuController<C> build() {
syncState,
ethProtocolManager);

final PluginServiceFactory additionalPluginServices =
createAdditionalPluginServices(blockchain);

final SubProtocolConfiguration subProtocolConfiguration =
createSubProtocolConfiguration(ethProtocolManager);

final JsonRpcMethodFactory additionalJsonRpcMethodFactory =
createAdditionalJsonRpcMethodFactory(protocolContext);

return new BesuController<>(
protocolSchedule,
protocolContext,
Expand All @@ -327,7 +331,8 @@ public BesuController<C> build() {
}
},
additionalJsonRpcMethodFactory,
nodeKeys);
nodeKeys,
additionalPluginServices);
}

protected void prepForBuild() {}
Expand Down Expand Up @@ -398,4 +403,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 @@ -50,6 +50,7 @@
import org.apache.logging.log4j.Logger;

public class CliqueBesuControllerBuilder extends BesuControllerBuilder<CliqueContext> {

private static final Logger LOG = LogManager.getLogger();

private Address localAddress;
Expand Down Expand Up @@ -139,6 +140,11 @@ protected void validateContext(final ProtocolContext<CliqueContext> context) {
}
}

@Override
protected PluginServiceFactory createAdditionalPluginServices(final Blockchain blockchain) {
return new CliqueQueryFactory(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,39 @@
/*
* 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.PoaQueryService;
import org.hyperledger.besu.services.BesuPluginContextImpl;

public class CliqueQueryFactory implements PluginServiceFactory {

final Blockchain blockchain;

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

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

besuContext.addService(
PoaQueryService.class, new PoaQueryServiceImpl(blockInterface, blockchain));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should register the one implementation instance of PoaQueryServiceImpl under both PoaQueryService and PoaMetricsService to preserve backwards compatibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ protected MiningCoordinator createMiningCoordinator(
return ibftMiningCoordinator;
}

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

@Override
protected ProtocolSchedule<IbftContext> createProtocolSchedule() {
return IbftProtocolSchedule.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,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,39 @@
/*
* 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.IbftQueryService;
import org.hyperledger.besu.services.BesuPluginContextImpl;

public class IbftQueryFactory implements PluginServiceFactory {

final Blockchain blockchain;

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

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

besuContext.addService(
IbftQueryService.class, new IbftQueryServiceImpl(blockInterface, blockchain));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should register the one implementation instance of PoaQueryServiceImpl under all of PoaQueryService, PoaMetricsService and IbftQueryService to preserve backwards compatibility.

This ensures users who are currently using pluginContext.getService(PoaMetricsService.class) or who want to have a plugin that works for both IBFT & Clique requesting PoaQueryService.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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 appendQueries(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 appendQueries(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 @@ -17,17 +17,17 @@
import org.hyperledger.besu.ethereum.chain.Blockchain;
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.metrics.PoaQueryService;

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

public class PoAMetricServiceImpl implements PoAMetricsService {
public class PoaQueryServiceImpl implements PoaQueryService {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also implement PoAMetricsService.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


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 +41,8 @@ public Collection<Address> getValidatorsForLatestBlock() {
public Address getProposerOfBlock(final BlockHeader header) {
return this.blockInterface.getProposerOfBlock(header);
}

protected Blockchain getBlockchain() {
return blockchain;
}
}
Loading