From 9826312383373467f37bc51114b5009299fb9c86 Mon Sep 17 00:00:00 2001 From: Mark Terry Date: Wed, 6 Nov 2019 09:29:38 +1000 Subject: [PATCH] PIE-2016: Added PoaQueryService method that returns local node signer address. Signed-off-by: Mark Terry --- .../controller/CliqueBesuControllerBuilder.java | 2 +- .../CliqueQueryPluginServiceFactory.java | 11 +++++++---- .../controller/IbftBesuControllerBuilder.java | 2 +- .../controller/IbftQueryPluginServiceFactory.java | 10 +++++++--- consensus/common/build.gradle | 1 + .../consensus/common/PoaQueryServiceImpl.java | 13 ++++++++++++- .../ibft/queries/IbftQueryServiceImpl.java | 6 ++++-- .../ibft/queries/IbftQueryServiceImplTest.java | 15 ++++++++++----- plugin-api/build.gradle | 2 +- .../plugin/services/query/PoaQueryService.java | 7 +++++++ 10 files changed, 51 insertions(+), 18 deletions(-) diff --git a/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java index ee70e2bc015..b12d3a77a5d 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java @@ -125,7 +125,7 @@ protected void validateContext(final ProtocolContext context) { @Override protected PluginServiceFactory createAdditionalPluginServices(final Blockchain blockchain) { - return new CliqueQueryPluginServiceFactory(blockchain); + return new CliqueQueryPluginServiceFactory(blockchain, nodeKeys); } @Override diff --git a/besu/src/main/java/org/hyperledger/besu/controller/CliqueQueryPluginServiceFactory.java b/besu/src/main/java/org/hyperledger/besu/controller/CliqueQueryPluginServiceFactory.java index 3099a24095e..57e2cb9f83d 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/CliqueQueryPluginServiceFactory.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/CliqueQueryPluginServiceFactory.java @@ -17,6 +17,7 @@ import org.hyperledger.besu.consensus.clique.CliqueBlockInterface; import org.hyperledger.besu.consensus.common.BlockInterface; import org.hyperledger.besu.consensus.common.PoaQueryServiceImpl; +import org.hyperledger.besu.crypto.SECP256K1.KeyPair; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.plugin.services.metrics.PoAMetricsService; import org.hyperledger.besu.plugin.services.query.PoaQueryService; @@ -24,17 +25,19 @@ public class CliqueQueryPluginServiceFactory implements PluginServiceFactory { - final Blockchain blockchain; + private final Blockchain blockchain; + private final KeyPair keyPair; - public CliqueQueryPluginServiceFactory(final Blockchain blockchain) { + public CliqueQueryPluginServiceFactory(final Blockchain blockchain, final KeyPair keyPair) { this.blockchain = blockchain; + this.keyPair = keyPair; } @Override public void appendPluginServices(final BesuPluginContextImpl besuContext) { final BlockInterface blockInterface = new CliqueBlockInterface(); - - final PoaQueryServiceImpl service = new PoaQueryServiceImpl(blockInterface, blockchain); + final PoaQueryServiceImpl service = + new PoaQueryServiceImpl(blockInterface, blockchain, keyPair); besuContext.addService(PoaQueryService.class, service); besuContext.addService(PoAMetricsService.class, service); } diff --git a/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java index 69a02796758..1e13790128a 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java @@ -192,7 +192,7 @@ protected MiningCoordinator createMiningCoordinator( @Override protected PluginServiceFactory createAdditionalPluginServices(final Blockchain blockchain) { - return new IbftQueryPluginServiceFactory(blockchain); + return new IbftQueryPluginServiceFactory(blockchain, nodeKeys); } @Override diff --git a/besu/src/main/java/org/hyperledger/besu/controller/IbftQueryPluginServiceFactory.java b/besu/src/main/java/org/hyperledger/besu/controller/IbftQueryPluginServiceFactory.java index fc0451a5670..2022a0b1bc8 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/IbftQueryPluginServiceFactory.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/IbftQueryPluginServiceFactory.java @@ -17,6 +17,7 @@ import org.hyperledger.besu.consensus.common.BlockInterface; import org.hyperledger.besu.consensus.ibft.IbftBlockInterface; import org.hyperledger.besu.consensus.ibft.queries.IbftQueryServiceImpl; +import org.hyperledger.besu.crypto.SECP256K1.KeyPair; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.plugin.services.metrics.PoAMetricsService; import org.hyperledger.besu.plugin.services.query.IbftQueryService; @@ -25,17 +26,20 @@ public class IbftQueryPluginServiceFactory implements PluginServiceFactory { - final Blockchain blockchain; + private final Blockchain blockchain; + private final KeyPair keyPair; - public IbftQueryPluginServiceFactory(final Blockchain blockchain) { + public IbftQueryPluginServiceFactory(final Blockchain blockchain, final KeyPair keyPair) { this.blockchain = blockchain; + this.keyPair = keyPair; } @Override public void appendPluginServices(final BesuPluginContextImpl besuContext) { final BlockInterface blockInterface = new IbftBlockInterface(); - final IbftQueryServiceImpl service = new IbftQueryServiceImpl(blockInterface, blockchain); + final IbftQueryServiceImpl service = + new IbftQueryServiceImpl(blockInterface, blockchain, keyPair); besuContext.addService(IbftQueryService.class, service); besuContext.addService(PoaQueryService.class, service); besuContext.addService(PoAMetricsService.class, service); diff --git a/consensus/common/build.gradle b/consensus/common/build.gradle index b6ade2ed0cc..c7acfccb2d2 100644 --- a/consensus/common/build.gradle +++ b/consensus/common/build.gradle @@ -32,6 +32,7 @@ dependencies { implementation project(':ethereum:core') implementation project(':ethereum:api') + implementation project(':crypto') implementation project(':util') implementation 'com.fasterxml.jackson.core:jackson-databind' diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/PoaQueryServiceImpl.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/PoaQueryServiceImpl.java index a7291728a2b..5bb96ae1005 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/PoaQueryServiceImpl.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/PoaQueryServiceImpl.java @@ -14,6 +14,7 @@ */ package org.hyperledger.besu.consensus.common; +import org.hyperledger.besu.crypto.SECP256K1.KeyPair; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.plugin.data.Address; import org.hyperledger.besu.plugin.data.BlockHeader; @@ -27,10 +28,15 @@ public class PoaQueryServiceImpl implements PoaQueryService, PoAMetricsService { private final BlockInterface blockInterface; private final Blockchain blockchain; + private final KeyPair keyPair; - public PoaQueryServiceImpl(final BlockInterface blockInterface, final Blockchain blockchain) { + public PoaQueryServiceImpl( + final BlockInterface blockInterface, + final Blockchain blockchain, + final KeyPair localNodeKeyPair) { this.blockInterface = blockInterface; this.blockchain = blockchain; + this.keyPair = localNodeKeyPair; } @Override @@ -46,4 +52,9 @@ public Address getProposerOfBlock(final BlockHeader header) { protected Blockchain getBlockchain() { return blockchain; } + + @Override + public Address getLocalSignerAddress() { + return org.hyperledger.besu.ethereum.core.Address.extract(keyPair.getPublicKey()); + } } diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/queries/IbftQueryServiceImpl.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/queries/IbftQueryServiceImpl.java index 8e1bec5acea..1ab9c159b66 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/queries/IbftQueryServiceImpl.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/queries/IbftQueryServiceImpl.java @@ -18,6 +18,7 @@ import org.hyperledger.besu.consensus.common.PoaQueryServiceImpl; import org.hyperledger.besu.consensus.ibft.IbftBlockHashing; import org.hyperledger.besu.consensus.ibft.IbftExtraData; +import org.hyperledger.besu.crypto.SECP256K1.KeyPair; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.Hash; @@ -30,8 +31,9 @@ public class IbftQueryServiceImpl extends PoaQueryServiceImpl implements IbftQueryService { - public IbftQueryServiceImpl(final BlockInterface blockInterface, final Blockchain blockchain) { - super(blockInterface, blockchain); + public IbftQueryServiceImpl( + final BlockInterface blockInterface, final Blockchain blockchain, final KeyPair keyPair) { + super(blockInterface, blockchain, keyPair); } @Override diff --git a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/queries/IbftQueryServiceImplTest.java b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/queries/IbftQueryServiceImplTest.java index a288492226d..5527ef22afc 100644 --- a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/queries/IbftQueryServiceImplTest.java +++ b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/queries/IbftQueryServiceImplTest.java @@ -48,6 +48,7 @@ public class IbftQueryServiceImplTest { private Blockchain blockchain = mock(Blockchain.class); + private KeyPair keyPair = mock(KeyPair.class); private final List validatorKeys = Lists.newArrayList(KeyPair.generate(), KeyPair.generate()); @@ -104,7 +105,8 @@ public void setup() { @Test public void roundNumberFromBlockIsReturned() { - final IbftQueryService service = new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain); + final IbftQueryService service = + new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain, keyPair); assertThat(service.getRoundNumberFrom(blockHeader)).isEqualTo(ROUND_NUMBER_IN_BLOCK); } @@ -115,14 +117,16 @@ public void getRoundNumberThrowsIfBlockIsNotOnTheChain() { new NonBesuBlockHeader(blockHeader.getHash(), blockHeader.getExtraData()); when(blockchain.getBlockHeader(blockHeader.getHash())).thenReturn(Optional.empty()); - final IbftQueryService service = new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain); + final IbftQueryService service = + new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain, keyPair); assertThatExceptionOfType(RuntimeException.class) .isThrownBy(() -> service.getRoundNumberFrom(header)); } @Test public void getSignersReturnsAddressesOfSignersInBlock() { - final IbftQueryService service = new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain); + final IbftQueryService service = + new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain, keyPair); final List
signers = signingKeys.stream() @@ -133,12 +137,13 @@ public void getSignersReturnsAddressesOfSignersInBlock() { } @Test - public void getSignersTheowsIfBlockIsNotOnTheChain() { + public void getSignersThrowsIfBlockIsNotOnTheChain() { final NonBesuBlockHeader header = new NonBesuBlockHeader(blockHeader.getHash(), blockHeader.getExtraData()); when(blockchain.getBlockHeader(blockHeader.getHash())).thenReturn(Optional.empty()); - final IbftQueryService service = new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain); + final IbftQueryService service = + new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain, keyPair); assertThatExceptionOfType(RuntimeException.class) .isThrownBy(() -> service.getSignersFrom(header)); } diff --git a/plugin-api/build.gradle b/plugin-api/build.gradle index 29266569caa..ce1e8a57c4f 100644 --- a/plugin-api/build.gradle +++ b/plugin-api/build.gradle @@ -56,7 +56,7 @@ Calculated : ${currentHash} tasks.register('checkAPIChanges', FileStateChecker) { description = "Checks that the API for the Plugin-API project does not change without deliberate thought" files = sourceSets.main.allJava.files - knownHash = 'EzyP5PAUCOxCe4TLjOD8igvg7LejQp/727hnvjBMlwY=' + knownHash = 'jcvmbyKAKVUK0Sr1RZXYvGmdhT8gqekgTwgrlWbmZ2I=' } check.dependsOn('checkAPIChanges') diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/query/PoaQueryService.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/query/PoaQueryService.java index 1404ba8aae2..85bb5e392dc 100644 --- a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/query/PoaQueryService.java +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/query/PoaQueryService.java @@ -36,4 +36,11 @@ public interface PoaQueryService { * @return The identity of the proposer for the given block. */ Address getProposerOfBlock(final BlockHeader header); + + /** + * Retrieves the signer {@link Address} of the local node. + * + * @return The signer {@link Address} of the local node. + */ + Address getLocalSignerAddress(); }