Skip to content

Commit

Permalink
PIE-2016: Added PoaQueryService method that returns local node signer…
Browse files Browse the repository at this point in the history
… address.

Signed-off-by: Mark Terry <mark.terry@consensys.net>
  • Loading branch information
mark-terry committed Nov 7, 2019
1 parent 1121c2a commit 9826312
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected void validateContext(final ProtocolContext<CliqueContext> context) {

@Override
protected PluginServiceFactory createAdditionalPluginServices(final Blockchain blockchain) {
return new CliqueQueryPluginServiceFactory(blockchain);
return new CliqueQueryPluginServiceFactory(blockchain, nodeKeys);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,27 @@
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;
import org.hyperledger.besu.services.BesuPluginContextImpl;

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ protected MiningCoordinator createMiningCoordinator(

@Override
protected PluginServiceFactory createAdditionalPluginServices(final Blockchain blockchain) {
return new IbftQueryPluginServiceFactory(blockchain);
return new IbftQueryPluginServiceFactory(blockchain, nodeKeys);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions consensus/common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
public class IbftQueryServiceImplTest {

private Blockchain blockchain = mock(Blockchain.class);
private KeyPair keyPair = mock(KeyPair.class);

private final List<KeyPair> validatorKeys =
Lists.newArrayList(KeyPair.generate(), KeyPair.generate());
Expand Down Expand Up @@ -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);
}
Expand All @@ -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<Address> signers =
signingKeys.stream()
Expand All @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion plugin-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 9826312

Please sign in to comment.