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

Redesign of how JsonRpcMethods are created #159

Merged
merged 4 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterRepository;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration;
import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketRequestHandler;
import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.Synchronizer;
Expand Down Expand Up @@ -52,7 +52,7 @@ public class BesuController<C> implements java.io.Closeable {
private final SubProtocolConfiguration subProtocolConfiguration;
private final KeyPair keyPair;
private final Synchronizer synchronizer;
private final JsonRpcMethodFactory additionalJsonRpcMethodsFactory;
private final JsonRpcMethods additionalJsonRpcMethodsFactory;

private final TransactionPool transactionPool;
private final MiningCoordinator miningCoordinator;
Expand All @@ -72,7 +72,7 @@ public class BesuController<C> implements java.io.Closeable {
final TransactionPool transactionPool,
final MiningCoordinator miningCoordinator,
final PrivacyParameters privacyParameters,
final JsonRpcMethodFactory additionalJsonRpcMethodsFactory,
final JsonRpcMethods additionalJsonRpcMethodsFactory,
final KeyPair keyPair,
final List<Closeable> closeables,
final PluginServiceFactory additionalPluginServices) {
Expand Down Expand Up @@ -147,7 +147,7 @@ public PrivacyParameters getPrivacyParameters() {

public Map<String, JsonRpcMethod> getAdditionalJsonRpcMethods(
final Collection<RpcApi> enabledRpcApis) {
return additionalJsonRpcMethodsFactory.createJsonRpcMethods(enabledRpcApis);
return additionalJsonRpcMethodsFactory.create(enabledRpcApis);
}

public SyncState getSyncState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.chain.GenesisState;
Expand Down Expand Up @@ -279,7 +279,7 @@ public BesuController<C> build() {
final SubProtocolConfiguration subProtocolConfiguration =
createSubProtocolConfiguration(ethProtocolManager);

final JsonRpcMethodFactory additionalJsonRpcMethodFactory =
final JsonRpcMethods additionalJsonRpcMethodFactory =
createAdditionalJsonRpcMethodFactory(protocolContext);

List<Closeable> closeables = new ArrayList<>();
Expand Down Expand Up @@ -307,7 +307,7 @@ public BesuController<C> build() {

protected void prepForBuild() {}

protected JsonRpcMethodFactory createAdditionalJsonRpcMethodFactory(
protected JsonRpcMethods createAdditionalJsonRpcMethodFactory(
final ProtocolContext<C> protocolContext) {
return apis -> Collections.emptyMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import org.hyperledger.besu.consensus.clique.blockcreation.CliqueBlockScheduler;
import org.hyperledger.besu.consensus.clique.blockcreation.CliqueMinerExecutor;
import org.hyperledger.besu.consensus.clique.blockcreation.CliqueMiningCoordinator;
import org.hyperledger.besu.consensus.clique.jsonrpc.CliqueJsonRpcMethodsFactory;
import org.hyperledger.besu.consensus.clique.jsonrpc.CliqueJsonRpcMethods;
import org.hyperledger.besu.consensus.common.BlockInterface;
import org.hyperledger.besu.consensus.common.EpochManager;
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.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.Address;
Expand Down Expand Up @@ -65,9 +65,9 @@ protected void prepForBuild() {
}

@Override
protected JsonRpcMethodFactory createAdditionalJsonRpcMethodFactory(
protected JsonRpcMethods createAdditionalJsonRpcMethodFactory(
final ProtocolContext<CliqueContext> protocolContext) {
return new CliqueJsonRpcMethodsFactory(protocolContext);
return new CliqueJsonRpcMethods(protocolContext);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.hyperledger.besu.consensus.ibft.blockcreation.IbftBlockCreatorFactory;
import org.hyperledger.besu.consensus.ibft.blockcreation.IbftMiningCoordinator;
import org.hyperledger.besu.consensus.ibft.blockcreation.ProposerSelector;
import org.hyperledger.besu.consensus.ibft.jsonrpc.IbftJsonRpcMethodsFactory;
import org.hyperledger.besu.consensus.ibft.jsonrpc.IbftJsonRpcMethods;
import org.hyperledger.besu.consensus.ibft.network.ValidatorPeers;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.consensus.ibft.protocol.IbftProtocolManager;
Expand All @@ -48,7 +48,7 @@
import org.hyperledger.besu.consensus.ibft.statemachine.IbftRoundFactory;
import org.hyperledger.besu.consensus.ibft.validation.MessageValidatorFactory;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.chain.MinedBlockObserver;
Expand Down Expand Up @@ -82,9 +82,9 @@ protected void prepForBuild() {
}

@Override
protected JsonRpcMethodFactory createAdditionalJsonRpcMethodFactory(
protected JsonRpcMethods createAdditionalJsonRpcMethodFactory(
final ProtocolContext<IbftContext> protocolContext) {
return new IbftJsonRpcMethodsFactory(protocolContext);
return new IbftJsonRpcMethods(protocolContext);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,55 +30,45 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.ApiGroupJsonRpcMethods;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;

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

public class CliqueJsonRpcMethodsFactory implements JsonRpcMethodFactory {
public class CliqueJsonRpcMethods extends ApiGroupJsonRpcMethods {

private final JsonRpcParameter parameter = new JsonRpcParameter();
private final ProtocolContext<CliqueContext> context;

public CliqueJsonRpcMethodsFactory(final ProtocolContext<CliqueContext> context) {
public CliqueJsonRpcMethods(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;
}
protected RpcApi getApiGroup() {
return CliqueRpcApis.CLIQUE;
}

@Override
protected Map<String, JsonRpcMethod> create() {
final MutableBlockchain blockchain = context.getBlockchain();
final WorldStateArchive worldStateArchive = context.getWorldStateArchive();
final BlockchainQueries blockchainQueries =
new BlockchainQueries(blockchain, worldStateArchive);
final VoteProposer voteProposer = context.getConsensusState().getVoteProposer();
final JsonRpcParameter jsonRpcParameter = new JsonRpcParameter();

// Must create our own voteTallyCache as using this would pollute the main voteTallyCache
final VoteTallyCache voteTallyCache = createVoteTallyCache(context, blockchain);

final CliqueGetSigners cliqueGetSigners =
new CliqueGetSigners(blockchainQueries, voteTallyCache, jsonRpcParameter);
final CliqueGetSignersAtHash cliqueGetSignersAtHash =
new CliqueGetSignersAtHash(blockchainQueries, voteTallyCache, jsonRpcParameter);
final Propose proposeRpc = new Propose(voteProposer, jsonRpcParameter);
final Discard discardRpc = new Discard(voteProposer, jsonRpcParameter);
final CliqueProposals cliqueProposals = new CliqueProposals(voteProposer);
final CliqueGetSignerMetrics cliqueGetSignerMetrics =
new CliqueGetSignerMetrics(new CliqueBlockInterface(), blockchainQueries, jsonRpcParameter);

rpcMethods.put(cliqueGetSigners.getName(), cliqueGetSigners);
rpcMethods.put(cliqueGetSignersAtHash.getName(), cliqueGetSignersAtHash);
rpcMethods.put(proposeRpc.getName(), proposeRpc);
rpcMethods.put(discardRpc.getName(), discardRpc);
rpcMethods.put(cliqueProposals.getName(), cliqueProposals);
rpcMethods.put(cliqueGetSignerMetrics.getName(), cliqueGetSignerMetrics);
return rpcMethods;
return mapOf(
new CliqueGetSigners(blockchainQueries, voteTallyCache, parameter),
new CliqueGetSignersAtHash(blockchainQueries, voteTallyCache, parameter),
new Propose(voteProposer, parameter),
new Discard(voteProposer, parameter),
new CliqueProposals(voteProposer),
new CliqueGetSignerMetrics(new CliqueBlockInterface(), blockchainQueries, parameter));
}

private VoteTallyCache createVoteTallyCache(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,38 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.ApiGroupJsonRpcMethods;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;

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

public class IbftJsonRpcMethodsFactory implements JsonRpcMethodFactory {
public class IbftJsonRpcMethods extends ApiGroupJsonRpcMethods {

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

public IbftJsonRpcMethodsFactory(final ProtocolContext<IbftContext> context) {
public IbftJsonRpcMethods(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)) {
final BlockchainQueries blockchainQueries =
new BlockchainQueries(context.getBlockchain(), context.getWorldStateArchive());
final VoteProposer voteProposer = context.getConsensusState().getVoteProposer();
final BlockInterface blockInterface = new IbftBlockInterface();

addMethods(
rpcMethods,
new IbftProposeValidatorVote(voteProposer, jsonRpcParameter),
new IbftGetValidatorsByBlockNumber(blockchainQueries, blockInterface, jsonRpcParameter),
new IbftDiscardValidatorVote(voteProposer, jsonRpcParameter),
new IbftGetValidatorsByBlockHash(
context.getBlockchain(), blockInterface, jsonRpcParameter),
new IbftGetSignerMetrics(blockInterface, blockchainQueries, jsonRpcParameter),
new IbftGetPendingVotes(voteProposer));
}

return rpcMethods;
protected RpcApi getApiGroup() {
return IbftRpcApis.IBFT;
}

private void addMethods(
final Map<String, JsonRpcMethod> methods, final JsonRpcMethod... rpcMethods) {
for (final JsonRpcMethod rpcMethod : rpcMethods) {
methods.put(rpcMethod.getName(), rpcMethod);
}
@Override
protected Map<String, JsonRpcMethod> create() {
final BlockchainQueries blockchainQueries =
new BlockchainQueries(context.getBlockchain(), context.getWorldStateArchive());
final VoteProposer voteProposer = context.getConsensusState().getVoteProposer();
final BlockInterface blockInterface = new IbftBlockInterface();

return mapOf(
new IbftProposeValidatorVote(voteProposer, jsonRpcParameter),
new IbftGetValidatorsByBlockNumber(blockchainQueries, blockInterface, jsonRpcParameter),
new IbftDiscardValidatorVote(voteProposer, jsonRpcParameter),
new IbftGetValidatorsByBlockHash(context.getBlockchain(), blockInterface, jsonRpcParameter),
new IbftGetSignerMetrics(blockInterface, blockchainQueries, jsonRpcParameter),
new IbftGetPendingVotes(voteProposer));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterRepository;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.method;
package org.hyperledger.besu.ethereum.api.jsonrpc.methods;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.method;
package org.hyperledger.besu.ethereum.api.jsonrpc.methods;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.method;
package org.hyperledger.besu.ethereum.api.jsonrpc.methods;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.method;
package org.hyperledger.besu.ethereum.api.jsonrpc.methods;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.method;
package org.hyperledger.besu.ethereum.api.jsonrpc.methods;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.method;
package org.hyperledger.besu.ethereum.api.jsonrpc.methods;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.method;
package org.hyperledger.besu.ethereum.api.jsonrpc.methods;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.method;
package org.hyperledger.besu.ethereum.api.jsonrpc.methods;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down
Loading