[NC-1402] Clique signer RPC methods to use current signers at specified block (#51)

Jason Frame 6 years ago committed by GitHub
parent 6ab32903ac
commit b039090b87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/CliqueContext.java
  2. 9
      consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/CliqueHelpers.java
  3. 24
      consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/CliqueJsonRpcMethodsFactory.java
  4. 20
      consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSigners.java
  5. 20
      consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHash.java
  6. 2
      consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/CliqueDifficultyCalculatorTest.java
  7. 14
      consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/NodeCanProduceNextBlockTest.java
  8. 2
      consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueBlockCreatorTest.java
  9. 2
      consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueMinerExecutorTest.java
  10. 2
      consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java
  11. 2
      consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/headervalidationrules/CliqueExtraDataValidationRuleTest.java
  12. 16
      consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHashTest.java
  13. 20
      consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersTest.java
  14. 3
      pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java

@ -1,5 +1,6 @@
package tech.pegasys.pantheon.consensus.clique; package tech.pegasys.pantheon.consensus.clique;
import tech.pegasys.pantheon.consensus.common.EpochManager;
import tech.pegasys.pantheon.consensus.common.VoteProposer; import tech.pegasys.pantheon.consensus.common.VoteProposer;
/** /**
@ -10,10 +11,15 @@ public class CliqueContext {
private final VoteTallyCache voteTallyCache; private final VoteTallyCache voteTallyCache;
private final VoteProposer voteProposer; private final VoteProposer voteProposer;
private final EpochManager epochManager;
public CliqueContext(final VoteTallyCache voteTallyCache, final VoteProposer voteProposer) { public CliqueContext(
final VoteTallyCache voteTallyCache,
final VoteProposer voteProposer,
final EpochManager epochManager) {
this.voteTallyCache = voteTallyCache; this.voteTallyCache = voteTallyCache;
this.voteProposer = voteProposer; this.voteProposer = voteProposer;
this.epochManager = epochManager;
} }
public VoteTallyCache getVoteTallyCache() { public VoteTallyCache getVoteTallyCache() {
@ -23,4 +29,8 @@ public class CliqueContext {
public VoteProposer getVoteProposer() { public VoteProposer getVoteProposer() {
return voteProposer; return voteProposer;
} }
public EpochManager getEpochManager() {
return epochManager;
}
} }

@ -7,9 +7,6 @@ import tech.pegasys.pantheon.ethereum.ProtocolContext;
import tech.pegasys.pantheon.ethereum.chain.Blockchain; import tech.pegasys.pantheon.ethereum.chain.Blockchain;
import tech.pegasys.pantheon.ethereum.core.Address; import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.BlockHeader; import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import java.util.List;
public class CliqueHelpers { public class CliqueHelpers {
@ -18,12 +15,6 @@ public class CliqueHelpers {
return CliqueBlockHashing.recoverProposerAddress(header, extraData); return CliqueBlockHashing.recoverProposerAddress(header, extraData);
} }
public static List<Address> getValidatorsOfBlock(final BlockHeader header) {
final BytesValue extraData = header.getExtraData();
final CliqueExtraData cliqueExtraData = CliqueExtraData.decode(extraData);
return cliqueExtraData.getValidators();
}
public static Address getProposerForBlockAfter( public static Address getProposerForBlockAfter(
final BlockHeader parent, final VoteTallyCache voteTallyCache) { final BlockHeader parent, final VoteTallyCache voteTallyCache) {
final CliqueProposerSelector proposerSelector = new CliqueProposerSelector(voteTallyCache); final CliqueProposerSelector proposerSelector = new CliqueProposerSelector(voteTallyCache);

@ -1,10 +1,14 @@
package tech.pegasys.pantheon.consensus.clique.jsonrpc; package tech.pegasys.pantheon.consensus.clique.jsonrpc;
import tech.pegasys.pantheon.consensus.clique.CliqueContext; import tech.pegasys.pantheon.consensus.clique.CliqueContext;
import tech.pegasys.pantheon.consensus.clique.CliqueVoteTallyUpdater;
import tech.pegasys.pantheon.consensus.clique.VoteTallyCache;
import tech.pegasys.pantheon.consensus.clique.jsonrpc.methods.CliqueGetSigners; import tech.pegasys.pantheon.consensus.clique.jsonrpc.methods.CliqueGetSigners;
import tech.pegasys.pantheon.consensus.clique.jsonrpc.methods.CliqueGetSignersAtHash; import tech.pegasys.pantheon.consensus.clique.jsonrpc.methods.CliqueGetSignersAtHash;
import tech.pegasys.pantheon.consensus.clique.jsonrpc.methods.Discard; import tech.pegasys.pantheon.consensus.clique.jsonrpc.methods.Discard;
import tech.pegasys.pantheon.consensus.clique.jsonrpc.methods.Propose; import tech.pegasys.pantheon.consensus.clique.jsonrpc.methods.Propose;
import tech.pegasys.pantheon.consensus.common.EpochManager;
import tech.pegasys.pantheon.consensus.common.VoteProposer;
import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.ProtocolContext;
import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain;
import tech.pegasys.pantheon.ethereum.db.WorldStateArchive; import tech.pegasys.pantheon.ethereum.db.WorldStateArchive;
@ -22,16 +26,17 @@ public class CliqueJsonRpcMethodsFactory {
final WorldStateArchive worldStateArchive = context.getWorldStateArchive(); final WorldStateArchive worldStateArchive = context.getWorldStateArchive();
final BlockchainQueries blockchainQueries = final BlockchainQueries blockchainQueries =
new BlockchainQueries(blockchain, worldStateArchive); new BlockchainQueries(blockchain, worldStateArchive);
final VoteProposer voteProposer = context.getConsensusState().getVoteProposer();
final JsonRpcParameter jsonRpcParameter = new JsonRpcParameter(); 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 = final CliqueGetSigners cliqueGetSigners =
new CliqueGetSigners(blockchainQueries, jsonRpcParameter); new CliqueGetSigners(blockchainQueries, voteTallyCache, jsonRpcParameter);
final CliqueGetSignersAtHash cliqueGetSignersAtHash = final CliqueGetSignersAtHash cliqueGetSignersAtHash =
new CliqueGetSignersAtHash(blockchainQueries, jsonRpcParameter); new CliqueGetSignersAtHash(blockchainQueries, voteTallyCache, jsonRpcParameter);
final Propose proposeRpc = final Propose proposeRpc = new Propose(voteProposer, jsonRpcParameter);
new Propose(context.getConsensusState().getVoteProposer(), jsonRpcParameter); final Discard discardRpc = new Discard(voteProposer, jsonRpcParameter);
final Discard discardRpc =
new Discard(context.getConsensusState().getVoteProposer(), jsonRpcParameter);
final Map<String, JsonRpcMethod> rpcMethods = new HashMap<>(); final Map<String, JsonRpcMethod> rpcMethods = new HashMap<>();
rpcMethods.put(cliqueGetSigners.getName(), cliqueGetSigners); rpcMethods.put(cliqueGetSigners.getName(), cliqueGetSigners);
@ -40,4 +45,11 @@ public class CliqueJsonRpcMethodsFactory {
rpcMethods.put(discardRpc.getName(), discardRpc); rpcMethods.put(discardRpc.getName(), discardRpc);
return rpcMethods; return rpcMethods;
} }
private VoteTallyCache createVoteTallyCache(
final ProtocolContext<CliqueContext> context, final MutableBlockchain blockchain) {
final EpochManager epochManager = context.getConsensusState().getEpochManager();
final CliqueVoteTallyUpdater cliqueVoteTallyUpdater = new CliqueVoteTallyUpdater(epochManager);
return new VoteTallyCache(blockchain, cliqueVoteTallyUpdater, epochManager);
}
} }

@ -1,7 +1,6 @@
package tech.pegasys.pantheon.consensus.clique.jsonrpc.methods; package tech.pegasys.pantheon.consensus.clique.jsonrpc.methods;
import static tech.pegasys.pantheon.consensus.clique.CliqueHelpers.getValidatorsOfBlock; import tech.pegasys.pantheon.consensus.clique.VoteTallyCache;
import tech.pegasys.pantheon.ethereum.core.BlockHeader; import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
@ -14,16 +13,22 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResp
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
public class CliqueGetSigners implements JsonRpcMethod { public class CliqueGetSigners implements JsonRpcMethod {
public static final String CLIQUE_GET_SIGNERS = "clique_getSigners"; public static final String CLIQUE_GET_SIGNERS = "clique_getSigners";
private final BlockchainQueries blockchainQueries; private final BlockchainQueries blockchainQueries;
private final VoteTallyCache voteTallyCache;
private final JsonRpcParameter parameters; private final JsonRpcParameter parameters;
public CliqueGetSigners( public CliqueGetSigners(
final BlockchainQueries blockchainQueries, final JsonRpcParameter parameter) { final BlockchainQueries blockchainQueries,
final VoteTallyCache voteTallyCache,
final JsonRpcParameter parameter) {
this.blockchainQueries = blockchainQueries; this.blockchainQueries = blockchainQueries;
this.voteTallyCache = voteTallyCache;
this.parameters = parameter; this.parameters = parameter;
} }
@ -34,14 +39,15 @@ public class CliqueGetSigners implements JsonRpcMethod {
@Override @Override
public JsonRpcResponse response(final JsonRpcRequest request) { public JsonRpcResponse response(final JsonRpcRequest request) {
final Optional<BlockHeader> blockHeader = blockHeader(request); final Optional<BlockHeader> blockHeader = determineBlockHeader(request);
return blockHeader return blockHeader
.<JsonRpcResponse>map( .map(bh -> voteTallyCache.getVoteTallyAtBlock(bh).getCurrentValidators())
bh -> new JsonRpcSuccessResponse(request.getId(), getValidatorsOfBlock(bh))) .map(addresses -> addresses.stream().map(Objects::toString).collect(Collectors.toList()))
.<JsonRpcResponse>map(addresses -> new JsonRpcSuccessResponse(request.getId(), addresses))
.orElse(new JsonRpcErrorResponse(request.getId(), JsonRpcError.INTERNAL_ERROR)); .orElse(new JsonRpcErrorResponse(request.getId(), JsonRpcError.INTERNAL_ERROR));
} }
private Optional<BlockHeader> blockHeader(final JsonRpcRequest request) { private Optional<BlockHeader> determineBlockHeader(final JsonRpcRequest request) {
final Optional<BlockParameter> blockParameter = final Optional<BlockParameter> blockParameter =
parameters.optional(request.getParams(), 0, BlockParameter.class); parameters.optional(request.getParams(), 0, BlockParameter.class);
final long latest = blockchainQueries.headBlockNumber(); final long latest = blockchainQueries.headBlockNumber();

@ -1,7 +1,6 @@
package tech.pegasys.pantheon.consensus.clique.jsonrpc.methods; package tech.pegasys.pantheon.consensus.clique.jsonrpc.methods;
import static tech.pegasys.pantheon.consensus.clique.CliqueHelpers.getValidatorsOfBlock; import tech.pegasys.pantheon.consensus.clique.VoteTallyCache;
import tech.pegasys.pantheon.ethereum.core.BlockHeader; import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.Hash; import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
@ -14,16 +13,22 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResp
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
public class CliqueGetSignersAtHash implements JsonRpcMethod { public class CliqueGetSignersAtHash implements JsonRpcMethod {
public static final String CLIQUE_GET_SIGNERS_AT_HASH = "clique_getSignersAtHash"; public static final String CLIQUE_GET_SIGNERS_AT_HASH = "clique_getSignersAtHash";
private final BlockchainQueries blockchainQueries; private final BlockchainQueries blockchainQueries;
private final VoteTallyCache voteTallyCache;
private final JsonRpcParameter parameters; private final JsonRpcParameter parameters;
public CliqueGetSignersAtHash( public CliqueGetSignersAtHash(
final BlockchainQueries blockchainQueries, final JsonRpcParameter parameter) { final BlockchainQueries blockchainQueries,
final VoteTallyCache voteTallyCache,
final JsonRpcParameter parameter) {
this.blockchainQueries = blockchainQueries; this.blockchainQueries = blockchainQueries;
this.voteTallyCache = voteTallyCache;
this.parameters = parameter; this.parameters = parameter;
} }
@ -34,14 +39,15 @@ public class CliqueGetSignersAtHash implements JsonRpcMethod {
@Override @Override
public JsonRpcResponse response(final JsonRpcRequest request) { public JsonRpcResponse response(final JsonRpcRequest request) {
final Optional<BlockHeader> blockHeader = blockHeader(request); final Optional<BlockHeader> blockHeader = determineBlockHeader(request);
return blockHeader return blockHeader
.<JsonRpcResponse>map( .map(bh -> voteTallyCache.getVoteTallyAtBlock(bh).getCurrentValidators())
bh -> new JsonRpcSuccessResponse(request.getId(), getValidatorsOfBlock(bh))) .map(addresses -> addresses.stream().map(Objects::toString).collect(Collectors.toList()))
.<JsonRpcResponse>map(addresses -> new JsonRpcSuccessResponse(request.getId(), addresses))
.orElse(new JsonRpcErrorResponse(request.getId(), JsonRpcError.INTERNAL_ERROR)); .orElse(new JsonRpcErrorResponse(request.getId(), JsonRpcError.INTERNAL_ERROR));
} }
private Optional<BlockHeader> blockHeader(final JsonRpcRequest request) { private Optional<BlockHeader> determineBlockHeader(final JsonRpcRequest request) {
final Hash hash = parameters.required(request.getParams(), 0, Hash.class); final Hash hash = parameters.required(request.getParams(), 0, Hash.class);
return blockchainQueries.blockByHash(hash).map(BlockWithMetadata::getHeader); return blockchainQueries.blockByHash(hash).map(BlockWithMetadata::getHeader);
} }

@ -42,7 +42,7 @@ public class CliqueDifficultyCalculatorTest {
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
final VoteProposer voteProposer = new VoteProposer(); final VoteProposer voteProposer = new VoteProposer();
final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null);
cliqueProtocolContext = new ProtocolContext<>(null, null, cliqueContext); cliqueProtocolContext = new ProtocolContext<>(null, null, cliqueContext);
blockHeaderBuilder = new BlockHeaderTestFixture(); blockHeaderBuilder = new BlockHeaderTestFixture();
} }

@ -68,7 +68,7 @@ public class NodeCanProduceNextBlockTest {
final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
final VoteProposer voteProposer = new VoteProposer(); final VoteProposer voteProposer = new VoteProposer();
final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null);
cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext); cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext);
headerBuilder.number(1).parentHash(genesisBlock.getHash()); headerBuilder.number(1).parentHash(genesisBlock.getHash());
@ -96,7 +96,7 @@ public class NodeCanProduceNextBlockTest {
final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
final VoteProposer voteProposer = new VoteProposer(); final VoteProposer voteProposer = new VoteProposer();
final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null);
cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext); cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext);
headerBuilder.number(1).parentHash(genesisBlock.getHash()); headerBuilder.number(1).parentHash(genesisBlock.getHash());
@ -133,7 +133,7 @@ public class NodeCanProduceNextBlockTest {
final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
final VoteProposer voteProposer = new VoteProposer(); final VoteProposer voteProposer = new VoteProposer();
final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null);
cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext); cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext);
headerBuilder.parentHash(genesisBlock.getHash()).number(1); headerBuilder.parentHash(genesisBlock.getHash()).number(1);
@ -166,7 +166,7 @@ public class NodeCanProduceNextBlockTest {
final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
final VoteProposer voteProposer = new VoteProposer(); final VoteProposer voteProposer = new VoteProposer();
final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null);
cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext); cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext);
headerBuilder.parentHash(genesisBlock.getHash()).number(1); headerBuilder.parentHash(genesisBlock.getHash()).number(1);
@ -214,7 +214,7 @@ public class NodeCanProduceNextBlockTest {
final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
final VoteProposer voteProposer = new VoteProposer(); final VoteProposer voteProposer = new VoteProposer();
final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null);
cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext); cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext);
headerBuilder.parentHash(genesisBlock.getHash()).number(1); headerBuilder.parentHash(genesisBlock.getHash()).number(1);
@ -246,7 +246,7 @@ public class NodeCanProduceNextBlockTest {
final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
final VoteProposer voteProposer = new VoteProposer(); final VoteProposer voteProposer = new VoteProposer();
final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null);
cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext); cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext);
headerBuilder.parentHash(Hash.ZERO).number(3); headerBuilder.parentHash(Hash.ZERO).number(3);
@ -273,7 +273,7 @@ public class NodeCanProduceNextBlockTest {
final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
final VoteProposer voteProposer = new VoteProposer(); final VoteProposer voteProposer = new VoteProposer();
final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null);
cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext); cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext);
headerBuilder.parentHash(Hash.ZERO).number(3); headerBuilder.parentHash(Hash.ZERO).number(3);

@ -79,7 +79,7 @@ public class CliqueBlockCreatorTest {
final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
voteProposer = new VoteProposer(); voteProposer = new VoteProposer();
final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null);
protocolContext = new ProtocolContext<>(blockchain, stateArchive, cliqueContext); protocolContext = new ProtocolContext<>(blockchain, stateArchive, cliqueContext);

@ -53,7 +53,7 @@ public class CliqueMinerExecutorTest {
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
final VoteProposer voteProposer = new VoteProposer(); final VoteProposer voteProposer = new VoteProposer();
final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null);
cliqueProtocolContext = new ProtocolContext<>(null, null, cliqueContext); cliqueProtocolContext = new ProtocolContext<>(null, null, cliqueContext);
blockHeaderBuilder = new BlockHeaderTestFixture(); blockHeaderBuilder = new BlockHeaderTestFixture();
} }

@ -47,7 +47,7 @@ public class CliqueDifficultyValidationRuleTest {
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
final VoteProposer voteProposer = new VoteProposer(); final VoteProposer voteProposer = new VoteProposer();
final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null);
cliqueProtocolContext = new ProtocolContext<>(null, null, cliqueContext); cliqueProtocolContext = new ProtocolContext<>(null, null, cliqueContext);
blockHeaderBuilder = new BlockHeaderTestFixture(); blockHeaderBuilder = new BlockHeaderTestFixture();
} }

@ -44,7 +44,7 @@ public class CliqueExtraDataValidationRuleTest {
final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class);
when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList));
final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, null); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, null, null);
cliqueProtocolContext = new ProtocolContext<>(null, null, cliqueContext); cliqueProtocolContext = new ProtocolContext<>(null, null, cliqueContext);
} }

@ -1,11 +1,14 @@
package tech.pegasys.pantheon.consensus.clique.jsonrpc.methods; package tech.pegasys.pantheon.consensus.clique.jsonrpc.methods;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static tech.pegasys.pantheon.ethereum.core.Address.fromHexString; import static tech.pegasys.pantheon.ethereum.core.Address.fromHexString;
import tech.pegasys.pantheon.consensus.clique.VoteTallyCache;
import tech.pegasys.pantheon.consensus.common.VoteTally;
import tech.pegasys.pantheon.ethereum.core.Address; import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.BlockHeader; import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture; import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture;
@ -38,16 +41,19 @@ public class CliqueGetSignersAtHashTest {
private CliqueGetSignersAtHash method; private CliqueGetSignersAtHash method;
private BlockHeader blockHeader; private BlockHeader blockHeader;
private List<Address> validators; private List<Address> validators;
private List<String> validatorsAsStrings;
@Mock private BlockchainQueries blockchainQueries; @Mock private BlockchainQueries blockchainQueries;
@Mock private VoteTallyCache voteTallyCache;
@Mock private BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata; @Mock private BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata;
@Mock private VoteTally voteTally;
public static final String BLOCK_HASH = public static final String BLOCK_HASH =
"0xe36a3edf0d8664002a72ef7c5f8e271485e7ce5c66455a07cb679d855818415f"; "0xe36a3edf0d8664002a72ef7c5f8e271485e7ce5c66455a07cb679d855818415f";
@Before @Before
public void setup() { public void setup() {
method = new CliqueGetSignersAtHash(blockchainQueries, new JsonRpcParameter()); method = new CliqueGetSignersAtHash(blockchainQueries, voteTallyCache, new JsonRpcParameter());
final byte[] genesisBlockExtraData = final byte[] genesisBlockExtraData =
Hex.decode( Hex.decode(
@ -61,6 +67,7 @@ public class CliqueGetSignersAtHashTest {
fromHexString("0x42eb768f2244c8811c63729a21a3569731535f06"), fromHexString("0x42eb768f2244c8811c63729a21a3569731535f06"),
fromHexString("0x7ffc57839b00206d1ad20c69a1981b489f772031"), fromHexString("0x7ffc57839b00206d1ad20c69a1981b489f772031"),
fromHexString("0xb279182d99e65703f0076e4812653aab85fca0f0")); fromHexString("0xb279182d99e65703f0076e4812653aab85fca0f0"));
validatorsAsStrings = validators.stream().map(Object::toString).collect(toList());
} }
@Test @Test
@ -90,10 +97,11 @@ public class CliqueGetSignersAtHashTest {
when(blockchainQueries.blockByHash(Hash.fromHexString(BLOCK_HASH))) when(blockchainQueries.blockByHash(Hash.fromHexString(BLOCK_HASH)))
.thenReturn(Optional.of(blockWithMetadata)); .thenReturn(Optional.of(blockWithMetadata));
when(blockWithMetadata.getHeader()).thenReturn(blockHeader); when(blockWithMetadata.getHeader()).thenReturn(blockHeader);
when(voteTallyCache.getVoteTallyAtBlock(blockHeader)).thenReturn(voteTally);
when(voteTally.getCurrentValidators()).thenReturn(validators);
final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request);
final List<Address> result = (List<Address>) response.getResult(); assertEquals(validatorsAsStrings, response.getResult());
assertEquals(validators, result);
} }
@Test @Test

@ -1,11 +1,14 @@
package tech.pegasys.pantheon.consensus.clique.jsonrpc.methods; package tech.pegasys.pantheon.consensus.clique.jsonrpc.methods;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static tech.pegasys.pantheon.ethereum.core.Address.fromHexString; import static tech.pegasys.pantheon.ethereum.core.Address.fromHexString;
import tech.pegasys.pantheon.consensus.clique.VoteTallyCache;
import tech.pegasys.pantheon.consensus.common.VoteTally;
import tech.pegasys.pantheon.ethereum.core.Address; import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.BlockHeader; import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture; import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture;
@ -35,14 +38,16 @@ public class CliqueGetSignersTest {
private CliqueGetSigners method; private CliqueGetSigners method;
private BlockHeader blockHeader; private BlockHeader blockHeader;
private List<Address> validators; private List<Address> validators;
private List<String> validatorAsStrings;
@Mock private BlockchainQueries blockchainQueries; @Mock private BlockchainQueries blockchainQueries;
@Mock private VoteTallyCache voteTallyCache;
@Mock private BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata; @Mock private BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata;
@Mock private VoteTally voteTally;
@Before @Before
public void setup() { public void setup() {
method = new CliqueGetSigners(blockchainQueries, new JsonRpcParameter()); method = new CliqueGetSigners(blockchainQueries, voteTallyCache, new JsonRpcParameter());
final byte[] genesisBlockExtraData = final byte[] genesisBlockExtraData =
Hex.decode( Hex.decode(
@ -56,6 +61,7 @@ public class CliqueGetSignersTest {
fromHexString("0x42eb768f2244c8811c63729a21a3569731535f06"), fromHexString("0x42eb768f2244c8811c63729a21a3569731535f06"),
fromHexString("0x7ffc57839b00206d1ad20c69a1981b489f772031"), fromHexString("0x7ffc57839b00206d1ad20c69a1981b489f772031"),
fromHexString("0xb279182d99e65703f0076e4812653aab85fca0f0")); fromHexString("0xb279182d99e65703f0076e4812653aab85fca0f0"));
validatorAsStrings = validators.stream().map(Object::toString).collect(toList());
} }
@Test @Test
@ -71,10 +77,11 @@ public class CliqueGetSignersTest {
when(blockchainQueries.headBlockNumber()).thenReturn(3065995L); when(blockchainQueries.headBlockNumber()).thenReturn(3065995L);
when(blockchainQueries.blockByNumber(3065995L)).thenReturn(Optional.of(blockWithMetadata)); when(blockchainQueries.blockByNumber(3065995L)).thenReturn(Optional.of(blockWithMetadata));
when(blockWithMetadata.getHeader()).thenReturn(blockHeader); when(blockWithMetadata.getHeader()).thenReturn(blockHeader);
when(voteTallyCache.getVoteTallyAtBlock(blockHeader)).thenReturn(voteTally);
when(voteTally.getCurrentValidators()).thenReturn(validators);
final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request);
final List<Address> result = (List<Address>) response.getResult(); assertEquals(validatorAsStrings, response.getResult());
assertEquals(validators, result);
} }
@Test @Test
@ -85,10 +92,11 @@ public class CliqueGetSignersTest {
when(blockchainQueries.blockByNumber(3065995L)).thenReturn(Optional.of(blockWithMetadata)); when(blockchainQueries.blockByNumber(3065995L)).thenReturn(Optional.of(blockWithMetadata));
when(blockWithMetadata.getHeader()).thenReturn(blockHeader); when(blockWithMetadata.getHeader()).thenReturn(blockHeader);
when(voteTallyCache.getVoteTallyAtBlock(blockHeader)).thenReturn(voteTally);
when(voteTally.getCurrentValidators()).thenReturn(validators);
final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request);
final List<Address> result = (List<Address>) response.getResult(); assertEquals(validatorAsStrings, response.getResult());
assertEquals(validators, result);
} }
@Test @Test

@ -114,7 +114,8 @@ public class CliquePantheonController
new CliqueContext( new CliqueContext(
new VoteTallyCache( new VoteTallyCache(
blockchain, new CliqueVoteTallyUpdater(epochManger), epochManger), blockchain, new CliqueVoteTallyUpdater(epochManger), epochManger),
new VoteProposer())); new VoteProposer(),
epochManger));
final SynchronizerConfiguration syncConfig = taintedSyncConfig.validated(blockchain); final SynchronizerConfiguration syncConfig = taintedSyncConfig.validated(blockchain);
final boolean fastSyncEnabled = syncConfig.syncMode().equals(SyncMode.FAST); final boolean fastSyncEnabled = syncConfig.syncMode().equals(SyncMode.FAST);

Loading…
Cancel
Save