Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Remove duplicate init code from PantheonController instances (#1305)
Browse files Browse the repository at this point in the history
Moves init code into a separate builder instead of a static init method, with common code in an abstract base class and subclasses of the builder for each of the consensus variants.
  • Loading branch information
ajsutton authored Apr 23, 2019
1 parent 1b445cd commit f05b2e6
Show file tree
Hide file tree
Showing 22 changed files with 974 additions and 1,421 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import tech.pegasys.pantheon.Runner;
import tech.pegasys.pantheon.RunnerBuilder;
import tech.pegasys.pantheon.cli.EthNetworkConfig;
import tech.pegasys.pantheon.cli.PantheonControllerBuilder;
import tech.pegasys.pantheon.controller.KeyPairUtil;
import tech.pegasys.pantheon.controller.PantheonController;
import tech.pegasys.pantheon.controller.PantheonControllerBuilder;
import tech.pegasys.pantheon.ethereum.eth.EthereumWireProtocolConfiguration;
import tech.pegasys.pantheon.ethereum.eth.sync.SynchronizerConfiguration;
import tech.pegasys.pantheon.ethereum.eth.transactions.PendingTransactions;
Expand All @@ -29,6 +29,7 @@

import java.io.IOException;
import java.nio.file.Path;
import java.time.Clock;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -55,28 +56,30 @@ public void startNode(final PantheonNode node) {
}

final MetricsSystem noOpMetricsSystem = new NoOpMetricsSystem();
final PantheonControllerBuilder builder = new PantheonControllerBuilder();
final EthNetworkConfig.Builder networkConfigBuilder =
new EthNetworkConfig.Builder(EthNetworkConfig.getNetworkConfig(DEV))
.setBootNodes(node.getConfiguration().bootnodes());
node.getConfiguration().getGenesisConfig().ifPresent(networkConfigBuilder::setGenesisConfig);
final EthNetworkConfig ethNetworkConfig = networkConfigBuilder.build();
final PantheonControllerBuilder<?> builder =
new PantheonController.Builder().fromEthNetworkConfig(ethNetworkConfig);
final Path tempDir = Files.createTempDir().toPath();

final PantheonController<?> pantheonController;
try {
pantheonController =
builder
.synchronizerConfiguration(new SynchronizerConfiguration.Builder().build())
.homePath(node.homeDirectory())
.ethNetworkConfig(ethNetworkConfig)
.dataDirectory(node.homeDirectory())
.miningParameters(node.getMiningParameters())
.privacyParameters(node.getPrivacyParameters())
.nodePrivateKeyFile(KeyPairUtil.getDefaultKeyFile(node.homeDirectory()))
.metricsSystem(noOpMetricsSystem)
.maxPendingTransactions(PendingTransactions.MAX_PENDING_TRANSACTIONS)
.rocksDbConfiguration(new RocksDbConfiguration.Builder().databaseDir(tempDir).build())
.rocksdDbConfiguration(
new RocksDbConfiguration.Builder().databaseDir(tempDir).build())
.ethereumWireProtocolConfiguration(EthereumWireProtocolConfiguration.defaultConfig())
.clock(Clock.systemUTC())
.build();
} catch (final IOException e) {
throw new RuntimeException("Error building PantheonController", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApi;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethodFactory;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.BlockchainQueries;
import tech.pegasys.pantheon.ethereum.worldstate.WorldStateArchive;
Expand All @@ -35,10 +36,16 @@
import java.util.HashMap;
import java.util.Map;

public class CliqueJsonRpcMethodsFactory {
public class CliqueJsonRpcMethodsFactory implements JsonRpcMethodFactory {

public Map<String, JsonRpcMethod> methods(
final ProtocolContext<CliqueContext> context, final Collection<RpcApi> jsonRpcApis) {
private final ProtocolContext<CliqueContext> context;

public CliqueJsonRpcMethodsFactory(final ProtocolContext<CliqueContext> context) {
this.context = context;
}

@Override
public Map<String, JsonRpcMethod> createJsonRpcMethods(final Collection<RpcApi> jsonRpcApis) {
final Map<String, JsonRpcMethod> rpcMethods = new HashMap<>();
if (!jsonRpcApis.contains(CliqueRpcApis.CLIQUE)) {
return rpcMethods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,25 @@
import tech.pegasys.pantheon.ethereum.ProtocolContext;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApi;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethodFactory;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.BlockchainQueries;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class IbftJsonRpcMethodsFactory {
public class IbftJsonRpcMethodsFactory implements JsonRpcMethodFactory {

private final JsonRpcParameter jsonRpcParameter = new JsonRpcParameter();
private final ProtocolContext<IbftContext> context;

public Map<String, JsonRpcMethod> methods(
final ProtocolContext<IbftContext> context, final Collection<RpcApi> jsonRpcApis) {
public IbftJsonRpcMethodsFactory(final ProtocolContext<IbftContext> context) {
this.context = context;
}

@Override
public Map<String, JsonRpcMethod> createJsonRpcMethods(final Collection<RpcApi> jsonRpcApis) {
final Map<String, JsonRpcMethod> rpcMethods = new HashMap<>();

if (jsonRpcApis.contains(IbftRpcApis.IBFT)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2019 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.
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods;

import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApi;

import java.util.Collection;
import java.util.Map;

public interface JsonRpcMethodFactory {
Map<String, JsonRpcMethod> createJsonRpcMethods(Collection<RpcApi> enabledRpcApis);
}
4 changes: 2 additions & 2 deletions pantheon/src/main/java/tech/pegasys/pantheon/Pantheon.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import static org.apache.logging.log4j.LogManager.getLogger;

import tech.pegasys.pantheon.cli.PantheonCommand;
import tech.pegasys.pantheon.cli.PantheonControllerBuilder;
import tech.pegasys.pantheon.controller.PantheonController;
import tech.pegasys.pantheon.ethereum.eth.EthereumWireProtocolConfiguration;
import tech.pegasys.pantheon.ethereum.eth.sync.SynchronizerConfiguration;
import tech.pegasys.pantheon.services.kvstore.RocksDbConfiguration;
Expand All @@ -34,7 +34,7 @@ public static void main(final String... args) {
getLogger(),
new BlockImporter(),
new RunnerBuilder(),
new PantheonControllerBuilder(),
new PantheonController.Builder(),
new SynchronizerConfiguration.Builder(),
EthereumWireProtocolConfiguration.builder(),
new RocksDbConfiguration.Builder());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public Runner build() {
final KeyPair keyPair = pantheonController.getLocalNodeKeyPair();

final SubProtocolConfiguration subProtocolConfiguration =
pantheonController.subProtocolConfiguration();
pantheonController.getSubProtocolConfiguration();

final ProtocolSchedule<?> protocolSchedule = pantheonController.getProtocolSchedule();
final ProtocolContext<?> context = pantheonController.getProtocolContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Clock;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -127,11 +128,11 @@ public class PantheonCommand implements DefaultCommandValues, Runnable {

private final BlockImporter blockImporter;

private final PantheonControllerBuilder controllerBuilder;
private final SynchronizerConfiguration.Builder synchronizerConfigurationBuilder;
private final EthereumWireProtocolConfiguration.Builder ethereumWireConfigurationBuilder;
private final RocksDbConfiguration.Builder rocksDbConfigurationBuilder;
private final RunnerBuilder runnerBuilder;
private final PantheonController.Builder controllerBuilderFactory;

protected KeyLoader getKeyLoader() {
return KeyPairUtil::loadKeyPair;
Expand Down Expand Up @@ -521,14 +522,14 @@ public PantheonCommand(
final Logger logger,
final BlockImporter blockImporter,
final RunnerBuilder runnerBuilder,
final PantheonControllerBuilder controllerBuilder,
final PantheonController.Builder controllerBuilderFactory,
final SynchronizerConfiguration.Builder synchronizerConfigurationBuilder,
final EthereumWireProtocolConfiguration.Builder ethereumWireConfigurationBuilder,
final RocksDbConfiguration.Builder rocksDbConfigurationBuilder) {
this.logger = logger;
this.blockImporter = blockImporter;
this.runnerBuilder = runnerBuilder;
this.controllerBuilder = controllerBuilder;
this.controllerBuilderFactory = controllerBuilderFactory;
this.synchronizerConfigurationBuilder = synchronizerConfigurationBuilder;
this.ethereumWireConfigurationBuilder = ethereumWireConfigurationBuilder;
this.rocksDbConfigurationBuilder = rocksDbConfigurationBuilder;
Expand Down Expand Up @@ -691,18 +692,19 @@ private void ensureAllNodesAreInWhitelist(

PantheonController<?> buildController() {
try {
return controllerBuilder
return controllerBuilderFactory
.fromEthNetworkConfig(updateNetworkConfig(getNetwork()))
.synchronizerConfiguration(buildSyncConfig())
.ethereumWireProtocolConfiguration(ethereumWireConfigurationBuilder.build())
.rocksDbConfiguration(buildRocksDbConfiguration())
.homePath(dataDir())
.ethNetworkConfig(updateNetworkConfig(getNetwork()))
.rocksdDbConfiguration(buildRocksDbConfiguration())
.dataDirectory(dataDir())
.miningParameters(
new MiningParameters(coinbase, minTransactionGasPrice, extraData, isMiningEnabled))
.maxPendingTransactions(txPoolMaxSize)
.nodePrivateKeyFile(nodePrivateKeyFile())
.metricsSystem(metricsSystem.get())
.privacyParameters(privacyParameters())
.clock(Clock.systemUTC())
.build();
} catch (final InvalidConfigurationException e) {
throw new ExecutionException(this.commandLine, e.getMessage());
Expand Down

This file was deleted.

Loading

0 comments on commit f05b2e6

Please sign in to comment.