From c5e78c369f94163c5d9cadb4be12d9cc0667448a Mon Sep 17 00:00:00 2001 From: Adrian Sutton Date: Wed, 21 Nov 2018 14:37:37 +1000 Subject: [PATCH] Delegate creation of consensus-specific JSON-RPC methods to the PantheonController instead of using instanceof and casting. (#289) --- .../jsonrpc/IbftJsonRpcMethodsFactory.java | 1 - .../tech/pegasys/pantheon/RunnerBuilder.java | 24 ++++--------------- .../controller/CliquePantheonController.java | 11 +++++++++ .../controller/IbftPantheonController.java | 11 +++++++++ .../controller/PantheonController.java | 11 +++++++++ 5 files changed, 37 insertions(+), 21 deletions(-) diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/jsonrpc/IbftJsonRpcMethodsFactory.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/jsonrpc/IbftJsonRpcMethodsFactory.java index 14bec82cbf..94c64f4fbf 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/jsonrpc/IbftJsonRpcMethodsFactory.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/jsonrpc/IbftJsonRpcMethodsFactory.java @@ -34,7 +34,6 @@ public class IbftJsonRpcMethodsFactory { final Map rpcMethods = new HashMap<>(); if (jsonRpcApis.contains(IbftRpcApis.IBFT)) { - // @formatter:off addMethods( rpcMethods, new IbftProposeValidatorVote( diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java b/pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java index 0ff1c0f472..2b3a5d65fe 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java @@ -12,10 +12,6 @@ */ package tech.pegasys.pantheon; -import tech.pegasys.pantheon.consensus.clique.CliqueContext; -import tech.pegasys.pantheon.consensus.clique.jsonrpc.CliqueJsonRpcMethodsFactory; -import tech.pegasys.pantheon.consensus.ibft.IbftContext; -import tech.pegasys.pantheon.consensus.ibft.jsonrpc.IbftJsonRpcMethodsFactory; import tech.pegasys.pantheon.controller.PantheonController; import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; import tech.pegasys.pantheon.ethereum.ProtocolContext; @@ -155,6 +151,7 @@ public class RunnerBuilder { jsonRpcMethods( context, protocolSchedule, + pantheonController, networkRunner, synchronizer, transactionPool, @@ -172,6 +169,7 @@ public class RunnerBuilder { jsonRpcMethods( context, protocolSchedule, + pantheonController, networkRunner, synchronizer, transactionPool, @@ -216,6 +214,7 @@ public class RunnerBuilder { private Map jsonRpcMethods( final ProtocolContext context, final ProtocolSchedule protocolSchedule, + final PantheonController pantheonController, final NetworkRunner networkRunner, final Synchronizer synchronizer, final TransactionPool transactionPool, @@ -237,22 +236,7 @@ public class RunnerBuilder { supportedCapabilities, jsonRpcApis, filterManager); - - if (context.getConsensusState() instanceof CliqueContext) { - // This is checked before entering this if branch - @SuppressWarnings("unchecked") - final ProtocolContext cliqueProtocolContext = - (ProtocolContext) context; - methods.putAll(new CliqueJsonRpcMethodsFactory().methods(cliqueProtocolContext, jsonRpcApis)); - } - - if (context.getConsensusState() instanceof IbftContext) { - // This is checked before entering this if branch - @SuppressWarnings("unchecked") - final ProtocolContext ibftProtocolContext = - (ProtocolContext) context; - methods.putAll(new IbftJsonRpcMethodsFactory().methods(ibftProtocolContext, jsonRpcApis)); - } + methods.putAll(pantheonController.getAdditionalJsonRpcMethods(jsonRpcApis)); return methods; } diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java b/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java index 915c51ba5c..b28d03c1e8 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java @@ -23,6 +23,7 @@ import tech.pegasys.pantheon.consensus.clique.VoteTallyCache; import tech.pegasys.pantheon.consensus.clique.blockcreation.CliqueBlockScheduler; import tech.pegasys.pantheon.consensus.clique.blockcreation.CliqueMinerExecutor; import tech.pegasys.pantheon.consensus.clique.blockcreation.CliqueMiningCoordinator; +import tech.pegasys.pantheon.consensus.clique.jsonrpc.CliqueJsonRpcMethodsFactory; import tech.pegasys.pantheon.consensus.common.EpochManager; import tech.pegasys.pantheon.consensus.common.VoteProposer; import tech.pegasys.pantheon.consensus.common.VoteTallyUpdater; @@ -46,6 +47,8 @@ import tech.pegasys.pantheon.ethereum.eth.sync.SyncMode; import tech.pegasys.pantheon.ethereum.eth.sync.SynchronizerConfiguration; import tech.pegasys.pantheon.ethereum.eth.sync.state.SyncState; import tech.pegasys.pantheon.ethereum.eth.transactions.TransactionPoolFactory; +import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApi; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod; import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; import tech.pegasys.pantheon.ethereum.p2p.api.ProtocolManager; import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration; @@ -54,6 +57,8 @@ import tech.pegasys.pantheon.ethereum.worldstate.WorldStateStorage; import java.io.IOException; import java.time.Clock; +import java.util.Collection; +import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; @@ -234,6 +239,12 @@ public class CliquePantheonController implements PantheonController getAdditionalJsonRpcMethods( + final Collection enabledRpcApis) { + return new CliqueJsonRpcMethodsFactory().methods(context, enabledRpcApis); + } + @Override public void close() { closer.run(); diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/controller/IbftPantheonController.java b/pantheon/src/main/java/tech/pegasys/pantheon/controller/IbftPantheonController.java index 9f0a5a4536..655c41361a 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/controller/IbftPantheonController.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/controller/IbftPantheonController.java @@ -27,6 +27,7 @@ import tech.pegasys.pantheon.consensus.ibft.IbftProcessor; import tech.pegasys.pantheon.consensus.ibft.IbftStateMachine; import tech.pegasys.pantheon.consensus.ibft.blockcreation.IbftBlockCreatorFactory; import tech.pegasys.pantheon.consensus.ibft.blockcreation.IbftMiningCoordinator; +import tech.pegasys.pantheon.consensus.ibft.jsonrpc.IbftJsonRpcMethodsFactory; import tech.pegasys.pantheon.consensus.ibft.network.IbftNetworkPeers; import tech.pegasys.pantheon.consensus.ibft.protocol.IbftProtocolManager; import tech.pegasys.pantheon.consensus.ibft.protocol.IbftSubProtocol; @@ -54,6 +55,8 @@ import tech.pegasys.pantheon.ethereum.eth.sync.SyncMode; import tech.pegasys.pantheon.ethereum.eth.sync.SynchronizerConfiguration; import tech.pegasys.pantheon.ethereum.eth.sync.state.SyncState; import tech.pegasys.pantheon.ethereum.eth.transactions.TransactionPoolFactory; +import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApi; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod; import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; import tech.pegasys.pantheon.ethereum.p2p.api.ProtocolManager; import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration; @@ -62,6 +65,8 @@ import tech.pegasys.pantheon.ethereum.storage.StorageProvider; import tech.pegasys.pantheon.ethereum.worldstate.WorldStateStorage; import java.io.IOException; +import java.util.Collection; +import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; @@ -267,6 +272,12 @@ public class IbftPantheonController implements PantheonController { return null; } + @Override + public Map getAdditionalJsonRpcMethods( + final Collection enabledRpcApis) { + return new IbftJsonRpcMethodsFactory().methods(context, enabledRpcApis); + } + @Override public void close() { closer.run(); diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/controller/PantheonController.java b/pantheon/src/main/java/tech/pegasys/pantheon/controller/PantheonController.java index ea2faa3c67..3dbbe8f5d6 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/controller/PantheonController.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/controller/PantheonController.java @@ -12,6 +12,8 @@ */ package tech.pegasys.pantheon.controller; +import static java.util.Collections.emptyMap; + import tech.pegasys.pantheon.config.GenesisConfigFile; import tech.pegasys.pantheon.config.GenesisConfigOptions; import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; @@ -21,12 +23,16 @@ import tech.pegasys.pantheon.ethereum.core.MiningParameters; import tech.pegasys.pantheon.ethereum.core.Synchronizer; import tech.pegasys.pantheon.ethereum.core.TransactionPool; import tech.pegasys.pantheon.ethereum.eth.sync.SynchronizerConfiguration; +import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApi; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod; import tech.pegasys.pantheon.ethereum.mainnet.MainnetProtocolSchedule; import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration; import tech.pegasys.pantheon.ethereum.storage.StorageProvider; import java.io.Closeable; +import java.util.Collection; +import java.util.Map; public interface PantheonController extends Closeable { @@ -81,4 +87,9 @@ public interface PantheonController extends Closeable { TransactionPool getTransactionPool(); MiningCoordinator getMiningCoordinator(); + + default Map getAdditionalJsonRpcMethods( + final Collection enabledRpcApis) { + return emptyMap(); + } }