diff --git a/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/CliqueContext.java b/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/CliqueContext.java index a79c96c334..6f956bf5ee 100644 --- a/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/CliqueContext.java +++ b/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/CliqueContext.java @@ -1,5 +1,6 @@ package tech.pegasys.pantheon.consensus.clique; +import tech.pegasys.pantheon.consensus.common.EpochManager; import tech.pegasys.pantheon.consensus.common.VoteProposer; /** @@ -10,10 +11,15 @@ public class CliqueContext { private final VoteTallyCache voteTallyCache; 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.voteProposer = voteProposer; + this.epochManager = epochManager; } public VoteTallyCache getVoteTallyCache() { @@ -23,4 +29,8 @@ public class CliqueContext { public VoteProposer getVoteProposer() { return voteProposer; } + + public EpochManager getEpochManager() { + return epochManager; + } } diff --git a/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/CliqueHelpers.java b/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/CliqueHelpers.java index aa761fe2f9..3c56ffe38f 100644 --- a/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/CliqueHelpers.java +++ b/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/CliqueHelpers.java @@ -7,9 +7,6 @@ import tech.pegasys.pantheon.ethereum.ProtocolContext; import tech.pegasys.pantheon.ethereum.chain.Blockchain; import tech.pegasys.pantheon.ethereum.core.Address; import tech.pegasys.pantheon.ethereum.core.BlockHeader; -import tech.pegasys.pantheon.util.bytes.BytesValue; - -import java.util.List; public class CliqueHelpers { @@ -18,12 +15,6 @@ public class CliqueHelpers { return CliqueBlockHashing.recoverProposerAddress(header, extraData); } - public static List
getValidatorsOfBlock(final BlockHeader header) { - final BytesValue extraData = header.getExtraData(); - final CliqueExtraData cliqueExtraData = CliqueExtraData.decode(extraData); - return cliqueExtraData.getValidators(); - } - public static Address getProposerForBlockAfter( final BlockHeader parent, final VoteTallyCache voteTallyCache) { final CliqueProposerSelector proposerSelector = new CliqueProposerSelector(voteTallyCache); diff --git a/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/CliqueJsonRpcMethodsFactory.java b/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/CliqueJsonRpcMethodsFactory.java index 995ae4d049..f331a2693d 100644 --- a/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/CliqueJsonRpcMethodsFactory.java +++ b/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/CliqueJsonRpcMethodsFactory.java @@ -1,10 +1,14 @@ package tech.pegasys.pantheon.consensus.clique.jsonrpc; 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.CliqueGetSignersAtHash; import tech.pegasys.pantheon.consensus.clique.jsonrpc.methods.Discard; 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.chain.MutableBlockchain; import tech.pegasys.pantheon.ethereum.db.WorldStateArchive; @@ -22,16 +26,17 @@ public class CliqueJsonRpcMethodsFactory { 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, jsonRpcParameter); + new CliqueGetSigners(blockchainQueries, voteTallyCache, jsonRpcParameter); final CliqueGetSignersAtHash cliqueGetSignersAtHash = - new CliqueGetSignersAtHash(blockchainQueries, jsonRpcParameter); - final Propose proposeRpc = - new Propose(context.getConsensusState().getVoteProposer(), jsonRpcParameter); - final Discard discardRpc = - new Discard(context.getConsensusState().getVoteProposer(), jsonRpcParameter); + new CliqueGetSignersAtHash(blockchainQueries, voteTallyCache, jsonRpcParameter); + final Propose proposeRpc = new Propose(voteProposer, jsonRpcParameter); + final Discard discardRpc = new Discard(voteProposer, jsonRpcParameter); final Map rpcMethods = new HashMap<>(); rpcMethods.put(cliqueGetSigners.getName(), cliqueGetSigners); @@ -40,4 +45,11 @@ public class CliqueJsonRpcMethodsFactory { rpcMethods.put(discardRpc.getName(), discardRpc); return rpcMethods; } + + private VoteTallyCache createVoteTallyCache( + final ProtocolContext context, final MutableBlockchain blockchain) { + final EpochManager epochManager = context.getConsensusState().getEpochManager(); + final CliqueVoteTallyUpdater cliqueVoteTallyUpdater = new CliqueVoteTallyUpdater(epochManager); + return new VoteTallyCache(blockchain, cliqueVoteTallyUpdater, epochManager); + } } diff --git a/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSigners.java b/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSigners.java index 15ed15ba9d..c5cf86e543 100644 --- a/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSigners.java +++ b/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSigners.java @@ -1,7 +1,6 @@ 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.jsonrpc.internal.JsonRpcRequest; 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.JsonRpcSuccessResponse; +import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; public class CliqueGetSigners implements JsonRpcMethod { public static final String CLIQUE_GET_SIGNERS = "clique_getSigners"; private final BlockchainQueries blockchainQueries; + private final VoteTallyCache voteTallyCache; private final JsonRpcParameter parameters; public CliqueGetSigners( - final BlockchainQueries blockchainQueries, final JsonRpcParameter parameter) { + final BlockchainQueries blockchainQueries, + final VoteTallyCache voteTallyCache, + final JsonRpcParameter parameter) { this.blockchainQueries = blockchainQueries; + this.voteTallyCache = voteTallyCache; this.parameters = parameter; } @@ -34,14 +39,15 @@ public class CliqueGetSigners implements JsonRpcMethod { @Override public JsonRpcResponse response(final JsonRpcRequest request) { - final Optional blockHeader = blockHeader(request); + final Optional blockHeader = determineBlockHeader(request); return blockHeader - .map( - bh -> new JsonRpcSuccessResponse(request.getId(), getValidatorsOfBlock(bh))) + .map(bh -> voteTallyCache.getVoteTallyAtBlock(bh).getCurrentValidators()) + .map(addresses -> addresses.stream().map(Objects::toString).collect(Collectors.toList())) + .map(addresses -> new JsonRpcSuccessResponse(request.getId(), addresses)) .orElse(new JsonRpcErrorResponse(request.getId(), JsonRpcError.INTERNAL_ERROR)); } - private Optional blockHeader(final JsonRpcRequest request) { + private Optional determineBlockHeader(final JsonRpcRequest request) { final Optional blockParameter = parameters.optional(request.getParams(), 0, BlockParameter.class); final long latest = blockchainQueries.headBlockNumber(); diff --git a/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHash.java b/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHash.java index 7f5ad9026d..d2ead9c9ca 100644 --- a/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHash.java +++ b/consensus/clique/src/main/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHash.java @@ -1,7 +1,6 @@ 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.Hash; 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.JsonRpcSuccessResponse; +import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; public class CliqueGetSignersAtHash implements JsonRpcMethod { public static final String CLIQUE_GET_SIGNERS_AT_HASH = "clique_getSignersAtHash"; private final BlockchainQueries blockchainQueries; + private final VoteTallyCache voteTallyCache; private final JsonRpcParameter parameters; public CliqueGetSignersAtHash( - final BlockchainQueries blockchainQueries, final JsonRpcParameter parameter) { + final BlockchainQueries blockchainQueries, + final VoteTallyCache voteTallyCache, + final JsonRpcParameter parameter) { this.blockchainQueries = blockchainQueries; + this.voteTallyCache = voteTallyCache; this.parameters = parameter; } @@ -34,14 +39,15 @@ public class CliqueGetSignersAtHash implements JsonRpcMethod { @Override public JsonRpcResponse response(final JsonRpcRequest request) { - final Optional blockHeader = blockHeader(request); + final Optional blockHeader = determineBlockHeader(request); return blockHeader - .map( - bh -> new JsonRpcSuccessResponse(request.getId(), getValidatorsOfBlock(bh))) + .map(bh -> voteTallyCache.getVoteTallyAtBlock(bh).getCurrentValidators()) + .map(addresses -> addresses.stream().map(Objects::toString).collect(Collectors.toList())) + .map(addresses -> new JsonRpcSuccessResponse(request.getId(), addresses)) .orElse(new JsonRpcErrorResponse(request.getId(), JsonRpcError.INTERNAL_ERROR)); } - private Optional blockHeader(final JsonRpcRequest request) { + private Optional determineBlockHeader(final JsonRpcRequest request) { final Hash hash = parameters.required(request.getParams(), 0, Hash.class); return blockchainQueries.blockByHash(hash).map(BlockWithMetadata::getHeader); } diff --git a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/CliqueDifficultyCalculatorTest.java b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/CliqueDifficultyCalculatorTest.java index da148ea5a5..7b952a2ec8 100644 --- a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/CliqueDifficultyCalculatorTest.java +++ b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/CliqueDifficultyCalculatorTest.java @@ -42,7 +42,7 @@ public class CliqueDifficultyCalculatorTest { when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); 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); blockHeaderBuilder = new BlockHeaderTestFixture(); } diff --git a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/NodeCanProduceNextBlockTest.java b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/NodeCanProduceNextBlockTest.java index 5e899f386b..74d6534a6e 100644 --- a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/NodeCanProduceNextBlockTest.java +++ b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/NodeCanProduceNextBlockTest.java @@ -68,7 +68,7 @@ public class NodeCanProduceNextBlockTest { final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); 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); headerBuilder.number(1).parentHash(genesisBlock.getHash()); @@ -96,7 +96,7 @@ public class NodeCanProduceNextBlockTest { final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); 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); headerBuilder.number(1).parentHash(genesisBlock.getHash()); @@ -133,7 +133,7 @@ public class NodeCanProduceNextBlockTest { final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); 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); headerBuilder.parentHash(genesisBlock.getHash()).number(1); @@ -166,7 +166,7 @@ public class NodeCanProduceNextBlockTest { final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); 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); headerBuilder.parentHash(genesisBlock.getHash()).number(1); @@ -214,7 +214,7 @@ public class NodeCanProduceNextBlockTest { final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); 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); headerBuilder.parentHash(genesisBlock.getHash()).number(1); @@ -246,7 +246,7 @@ public class NodeCanProduceNextBlockTest { final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); 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); headerBuilder.parentHash(Hash.ZERO).number(3); @@ -273,7 +273,7 @@ public class NodeCanProduceNextBlockTest { final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); 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); headerBuilder.parentHash(Hash.ZERO).number(3); diff --git a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueBlockCreatorTest.java b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueBlockCreatorTest.java index db28f122c3..665a8fc3f2 100644 --- a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueBlockCreatorTest.java +++ b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueBlockCreatorTest.java @@ -79,7 +79,7 @@ public class CliqueBlockCreatorTest { final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); voteProposer = new VoteProposer(); - final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer); + final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null); protocolContext = new ProtocolContext<>(blockchain, stateArchive, cliqueContext); diff --git a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueMinerExecutorTest.java b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueMinerExecutorTest.java index 7f7ea6cba0..11a10be163 100644 --- a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueMinerExecutorTest.java +++ b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/blockcreation/CliqueMinerExecutorTest.java @@ -53,7 +53,7 @@ public class CliqueMinerExecutorTest { when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); 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); blockHeaderBuilder = new BlockHeaderTestFixture(); } diff --git a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java index 07c5fe8ac3..4a48e3961c 100644 --- a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java +++ b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java @@ -47,7 +47,7 @@ public class CliqueDifficultyValidationRuleTest { when(voteTallyCache.getVoteTallyAtBlock(any())).thenReturn(new VoteTally(validatorList)); 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); blockHeaderBuilder = new BlockHeaderTestFixture(); } diff --git a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/headervalidationrules/CliqueExtraDataValidationRuleTest.java b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/headervalidationrules/CliqueExtraDataValidationRuleTest.java index 307295c5bc..da5fe5f406 100644 --- a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/headervalidationrules/CliqueExtraDataValidationRuleTest.java +++ b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/headervalidationrules/CliqueExtraDataValidationRuleTest.java @@ -44,7 +44,7 @@ public class CliqueExtraDataValidationRuleTest { final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); 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); } diff --git a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHashTest.java b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHashTest.java index ba8c41ff6f..f07bf90571 100644 --- a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHashTest.java +++ b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHashTest.java @@ -1,11 +1,14 @@ package tech.pegasys.pantheon.consensus.clique.jsonrpc.methods; import static java.util.Arrays.asList; +import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.when; 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.BlockHeader; import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture; @@ -38,16 +41,19 @@ public class CliqueGetSignersAtHashTest { private CliqueGetSignersAtHash method; private BlockHeader blockHeader; private List
validators; + private List validatorsAsStrings; @Mock private BlockchainQueries blockchainQueries; - + @Mock private VoteTallyCache voteTallyCache; @Mock private BlockWithMetadata blockWithMetadata; + @Mock private VoteTally voteTally; + public static final String BLOCK_HASH = "0xe36a3edf0d8664002a72ef7c5f8e271485e7ce5c66455a07cb679d855818415f"; @Before public void setup() { - method = new CliqueGetSignersAtHash(blockchainQueries, new JsonRpcParameter()); + method = new CliqueGetSignersAtHash(blockchainQueries, voteTallyCache, new JsonRpcParameter()); final byte[] genesisBlockExtraData = Hex.decode( @@ -61,6 +67,7 @@ public class CliqueGetSignersAtHashTest { fromHexString("0x42eb768f2244c8811c63729a21a3569731535f06"), fromHexString("0x7ffc57839b00206d1ad20c69a1981b489f772031"), fromHexString("0xb279182d99e65703f0076e4812653aab85fca0f0")); + validatorsAsStrings = validators.stream().map(Object::toString).collect(toList()); } @Test @@ -90,10 +97,11 @@ public class CliqueGetSignersAtHashTest { when(blockchainQueries.blockByHash(Hash.fromHexString(BLOCK_HASH))) .thenReturn(Optional.of(blockWithMetadata)); when(blockWithMetadata.getHeader()).thenReturn(blockHeader); + when(voteTallyCache.getVoteTallyAtBlock(blockHeader)).thenReturn(voteTally); + when(voteTally.getCurrentValidators()).thenReturn(validators); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); - final List
result = (List
) response.getResult(); - assertEquals(validators, result); + assertEquals(validatorsAsStrings, response.getResult()); } @Test diff --git a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersTest.java b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersTest.java index e4e2aea106..05d6bb5cd9 100644 --- a/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersTest.java +++ b/consensus/clique/src/test/java/tech/pegasys/pantheon/consensus/clique/jsonrpc/methods/CliqueGetSignersTest.java @@ -1,11 +1,14 @@ package tech.pegasys.pantheon.consensus.clique.jsonrpc.methods; import static java.util.Arrays.asList; +import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.when; 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.BlockHeader; import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture; @@ -35,14 +38,16 @@ public class CliqueGetSignersTest { private CliqueGetSigners method; private BlockHeader blockHeader; private List
validators; + private List validatorAsStrings; @Mock private BlockchainQueries blockchainQueries; - + @Mock private VoteTallyCache voteTallyCache; @Mock private BlockWithMetadata blockWithMetadata; + @Mock private VoteTally voteTally; @Before public void setup() { - method = new CliqueGetSigners(blockchainQueries, new JsonRpcParameter()); + method = new CliqueGetSigners(blockchainQueries, voteTallyCache, new JsonRpcParameter()); final byte[] genesisBlockExtraData = Hex.decode( @@ -56,6 +61,7 @@ public class CliqueGetSignersTest { fromHexString("0x42eb768f2244c8811c63729a21a3569731535f06"), fromHexString("0x7ffc57839b00206d1ad20c69a1981b489f772031"), fromHexString("0xb279182d99e65703f0076e4812653aab85fca0f0")); + validatorAsStrings = validators.stream().map(Object::toString).collect(toList()); } @Test @@ -71,10 +77,11 @@ public class CliqueGetSignersTest { when(blockchainQueries.headBlockNumber()).thenReturn(3065995L); when(blockchainQueries.blockByNumber(3065995L)).thenReturn(Optional.of(blockWithMetadata)); when(blockWithMetadata.getHeader()).thenReturn(blockHeader); + when(voteTallyCache.getVoteTallyAtBlock(blockHeader)).thenReturn(voteTally); + when(voteTally.getCurrentValidators()).thenReturn(validators); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); - final List
result = (List
) response.getResult(); - assertEquals(validators, result); + assertEquals(validatorAsStrings, response.getResult()); } @Test @@ -85,10 +92,11 @@ public class CliqueGetSignersTest { when(blockchainQueries.blockByNumber(3065995L)).thenReturn(Optional.of(blockWithMetadata)); when(blockWithMetadata.getHeader()).thenReturn(blockHeader); + when(voteTallyCache.getVoteTallyAtBlock(blockHeader)).thenReturn(voteTally); + when(voteTally.getCurrentValidators()).thenReturn(validators); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); - final List
result = (List
) response.getResult(); - assertEquals(validators, result); + assertEquals(validatorAsStrings, response.getResult()); } @Test 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 0d0fe15948..5125b59295 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java @@ -114,7 +114,8 @@ public class CliquePantheonController new CliqueContext( new VoteTallyCache( blockchain, new CliqueVoteTallyUpdater(epochManger), epochManger), - new VoteProposer())); + new VoteProposer(), + epochManger)); final SynchronizerConfiguration syncConfig = taintedSyncConfig.validated(blockchain); final boolean fastSyncEnabled = syncConfig.syncMode().equals(SyncMode.FAST);