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
#163)

* PIE-2016: Added PoaQueryService method that returns local node signer address.

Signed-off-by: Mark Terry <mark.terry@consensys.net>

* [PIE-2016] PR fixes.

Signed-off-by: Mark Terry <mark.terry@consensys.net>
  • Loading branch information
mark-terry authored Nov 8, 2019
1 parent 1121c2a commit 4eff8de
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 20 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,28 @@
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 localNodeKeypair;

public CliqueQueryPluginServiceFactory(final Blockchain blockchain) {
public CliqueQueryPluginServiceFactory(
final Blockchain blockchain, final KeyPair localNodeKeypair) {
this.blockchain = blockchain;
this.localNodeKeypair = localNodeKeypair;
}

@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, localNodeKeypair);
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,21 @@

public class IbftQueryPluginServiceFactory implements PluginServiceFactory {

final Blockchain blockchain;
private final Blockchain blockchain;
private final KeyPair localNodeKeypair;

public IbftQueryPluginServiceFactory(final Blockchain blockchain) {
public IbftQueryPluginServiceFactory(
final Blockchain blockchain, final KeyPair localNodeKeypair) {
this.blockchain = blockchain;
this.localNodeKeypair = localNodeKeypair;
}

@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, localNodeKeypair);
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 localNodeKeypair;

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.localNodeKeypair = 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(localNodeKeypair.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 @@ -16,7 +16,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.consensus.ibft.IbftBlockHashing;
Expand Down Expand Up @@ -44,10 +43,15 @@
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class IbftQueryServiceImplTest {

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

private final List<KeyPair> validatorKeys =
Lists.newArrayList(KeyPair.generate(), KeyPair.generate());
Expand Down Expand Up @@ -104,7 +108,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 +120,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 +140,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 = 'Uz20ItZSHoOEJdONKKwUtoyZxkCVF+bbw20IMmZfSZg='
}
check.dependsOn('checkAPIChanges')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ 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. This is the address added to the {@link
* BlockHeader} to identify that a specific node was a validator during block creation.
*
* @return The signer {@link Address} of the local node.
*/
Address getLocalSignerAddress();
}

0 comments on commit 4eff8de

Please sign in to comment.