Move IBFT To using injectable crypto (#618)

Currently, Besu injects a KeyPair from the initial entry, through the hierarchy of classes and into various leaf classes. The leaf classes then execute statically defined cryptographic functions (eg Secp256k1.sign) using the injected key.

Thus the implementation of the cryptographic function is hardcoded by the leaf node, and cannot be injected - and thus its implementation cannot be altered.

This change combines the node's KeyPair with associated cryptographic functions in a single class, which can then be injected. This in turn enables other cryptographic backends to be utilised by Besu.

Signed-off-by: Trent Mohay <trent.mohay@consensys.net>
pull/665/head
Trent Mohay 5 years ago committed by GitHub
parent c58cb7c67e
commit 5770e3b8ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java
  2. 11
      besu/src/main/java/org/hyperledger/besu/controller/CliqueQueryPluginServiceFactory.java
  3. 15
      besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java
  4. 11
      besu/src/main/java/org/hyperledger/besu/controller/IbftQueryPluginServiceFactory.java
  5. 12
      consensus/common/src/main/java/org/hyperledger/besu/consensus/common/PoaQueryServiceImpl.java
  6. 12
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/IntegrationTestHelpers.java
  7. 9
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/NetworkLayout.java
  8. 12
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/NodeParams.java
  9. 2
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/TestContext.java
  10. 18
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/TestContextBuilder.java
  11. 21
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/support/ValidatorPeer.java
  12. 10
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/tests/FutureHeightTest.java
  13. 3
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/tests/FutureRoundTest.java
  14. 5
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/tests/GossipTest.java
  15. 2
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/tests/LocalNodeIsProposerTest.java
  16. 2
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/tests/LocalNodeNotProposerTest.java
  17. 2
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/tests/ReceivedFutureProposalTest.java
  18. 15
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/tests/SpuriousBehaviourTest.java
  19. 18
      consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/payload/MessageFactory.java
  20. 6
      consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/queries/IbftQueryServiceImpl.java
  21. 12
      consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/statemachine/IbftFinalState.java
  22. 11
      consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/statemachine/IbftRound.java
  23. 2
      consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/statemachine/IbftRoundFactory.java
  24. 26
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftBlockHashingTest.java
  25. 100
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftBlockHeaderValidationRulesetFactoryTest.java
  26. 17
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftExtraDataFixture.java
  27. 9
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftGossipTest.java
  28. 7
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftHelpersTest.java
  29. 16
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/TestHelpers.java
  30. 6
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/headervalidationrules/HeaderValidationTestHelpers.java
  31. 29
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/headervalidationrules/IbftCoinbaseValidationRuleTest.java
  32. 41
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/headervalidationrules/IbftCommitSealsValidationRuleTest.java
  33. 24
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetSignerMetricsTest.java
  34. 28
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/queries/IbftQueryServiceImplTest.java
  35. 17
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/statemachine/IbftBlockHeightManagerTest.java
  36. 36
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/statemachine/IbftRoundTest.java
  37. 7
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/statemachine/RoundChangeArtifactsTest.java
  38. 17
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/statemachine/RoundChangeManagerTest.java
  39. 13
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/statemachine/RoundStateTest.java
  40. 5
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/validation/FutureRoundProposalMessageValidatorTest.java
  41. 10
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/validation/MessageValidatorTest.java
  42. 6
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/validation/ProposalBlockConsistencyValidatorTest.java
  43. 9
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/validation/RoundChangeCertificateValidatorTest.java
  44. 5
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/validation/RoundChangeMessageValidatorTest.java
  45. 9
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/validation/RoundChangeSignedDataValidatorTest.java
  46. 20
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/validation/SignedDataValidatorTest.java
  47. 44
      crypto/src/main/java/org/hyperledger/besu/crypto/BouncyCastleNodeKey.java
  48. 27
      crypto/src/main/java/org/hyperledger/besu/crypto/NodeKey.java
  49. 84
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/precompiles/privacy/PrivacyPrecompiledContractTest.java

@ -28,6 +28,7 @@ import org.hyperledger.besu.consensus.common.EpochManager;
import org.hyperledger.besu.consensus.common.VoteProposer;
import org.hyperledger.besu.consensus.common.VoteTallyCache;
import org.hyperledger.besu.consensus.common.VoteTallyUpdater;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
@ -46,6 +47,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class CliqueBesuControllerBuilder extends BesuControllerBuilder<CliqueContext> {
private static final Logger LOG = LogManager.getLogger();
private Address localAddress;
@ -125,7 +127,7 @@ public class CliqueBesuControllerBuilder extends BesuControllerBuilder<CliqueCon
@Override
protected PluginServiceFactory createAdditionalPluginServices(final Blockchain blockchain) {
return new CliqueQueryPluginServiceFactory(blockchain, nodeKeys);
return new CliqueQueryPluginServiceFactory(blockchain, new BouncyCastleNodeKey(nodeKeys));
}
@Override

@ -17,7 +17,7 @@ package org.hyperledger.besu.controller;
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.crypto.NodeKey;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.plugin.services.metrics.PoAMetricsService;
import org.hyperledger.besu.plugin.services.query.PoaQueryService;
@ -26,19 +26,18 @@ import org.hyperledger.besu.services.BesuPluginContextImpl;
public class CliqueQueryPluginServiceFactory implements PluginServiceFactory {
private final Blockchain blockchain;
private final KeyPair localNodeKeypair;
private final NodeKey nodeKey;
public CliqueQueryPluginServiceFactory(
final Blockchain blockchain, final KeyPair localNodeKeypair) {
public CliqueQueryPluginServiceFactory(final Blockchain blockchain, final NodeKey nodeKey) {
this.blockchain = blockchain;
this.localNodeKeypair = localNodeKeypair;
this.nodeKey = nodeKey;
}
@Override
public void appendPluginServices(final BesuPluginContextImpl besuContext) {
final BlockInterface blockInterface = new CliqueBlockInterface();
final PoaQueryServiceImpl service =
new PoaQueryServiceImpl(blockInterface, blockchain, localNodeKeypair);
new PoaQueryServiceImpl(blockInterface, blockchain, nodeKey);
besuContext.addService(PoaQueryService.class, service);
besuContext.addService(PoAMetricsService.class, service);
}

@ -51,6 +51,8 @@ import org.hyperledger.besu.consensus.ibft.statemachine.IbftController;
import org.hyperledger.besu.consensus.ibft.statemachine.IbftFinalState;
import org.hyperledger.besu.consensus.ibft.statemachine.IbftRoundFactory;
import org.hyperledger.besu.consensus.ibft.validation.MessageValidatorFactory;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
@ -117,6 +119,8 @@ public class IbftBesuControllerBuilder extends BesuControllerBuilder<IbftContext
final MutableBlockchain blockchain = protocolContext.getBlockchain();
final IbftExecutors ibftExecutors = IbftExecutors.create(metricsSystem);
final NodeKey nodeKey = new BouncyCastleNodeKey(nodeKeys);
final IbftBlockCreatorFactory blockCreatorFactory =
new IbftBlockCreatorFactory(
gasLimitCalculator,
@ -124,7 +128,7 @@ public class IbftBesuControllerBuilder extends BesuControllerBuilder<IbftContext
protocolContext,
protocolSchedule,
miningParameters,
Util.publicKeyToAddress(nodeKeys.getPublicKey()));
Util.publicKeyToAddress(nodeKey.getPublicKey()));
// NOTE: peers should not be used for accessing the network as it does not enforce the
// "only send once" filter applied by the UniqueMessageMulticaster.
@ -143,15 +147,15 @@ public class IbftBesuControllerBuilder extends BesuControllerBuilder<IbftContext
final IbftFinalState finalState =
new IbftFinalState(
voteTallyCache,
nodeKeys,
Util.publicKeyToAddress(nodeKeys.getPublicKey()),
nodeKey,
Util.publicKeyToAddress(nodeKey.getPublicKey()),
proposerSelector,
uniqueMessageMulticaster,
new RoundTimer(ibftEventQueue, ibftConfig.getRequestTimeoutSeconds(), ibftExecutors),
new BlockTimer(
ibftEventQueue, ibftConfig.getBlockPeriodSeconds(), ibftExecutors, clock),
blockCreatorFactory,
new MessageFactory(nodeKeys),
new MessageFactory(nodeKey),
clock);
final MessageValidatorFactory messageValidatorFactory =
@ -204,7 +208,8 @@ public class IbftBesuControllerBuilder extends BesuControllerBuilder<IbftContext
@Override
protected PluginServiceFactory createAdditionalPluginServices(final Blockchain blockchain) {
return new IbftQueryPluginServiceFactory(blockchain, nodeKeys);
final NodeKey nodeKey = new BouncyCastleNodeKey(nodeKeys);
return new IbftQueryPluginServiceFactory(blockchain, nodeKey);
}
@Override

@ -17,7 +17,7 @@ package org.hyperledger.besu.controller;
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.crypto.NodeKey;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.plugin.services.metrics.PoAMetricsService;
import org.hyperledger.besu.plugin.services.query.IbftQueryService;
@ -27,12 +27,11 @@ import org.hyperledger.besu.services.BesuPluginContextImpl;
public class IbftQueryPluginServiceFactory implements PluginServiceFactory {
private final Blockchain blockchain;
private final KeyPair localNodeKeypair;
private final NodeKey nodeKey;
public IbftQueryPluginServiceFactory(
final Blockchain blockchain, final KeyPair localNodeKeypair) {
public IbftQueryPluginServiceFactory(final Blockchain blockchain, final NodeKey nodeKey) {
this.blockchain = blockchain;
this.localNodeKeypair = localNodeKeypair;
this.nodeKey = nodeKey;
}
@Override
@ -40,7 +39,7 @@ public class IbftQueryPluginServiceFactory implements PluginServiceFactory {
final BlockInterface blockInterface = new IbftBlockInterface();
final IbftQueryServiceImpl service =
new IbftQueryServiceImpl(blockInterface, blockchain, localNodeKeypair);
new IbftQueryServiceImpl(blockInterface, blockchain, nodeKey);
besuContext.addService(IbftQueryService.class, service);
besuContext.addService(PoaQueryService.class, service);
besuContext.addService(PoAMetricsService.class, service);

@ -14,7 +14,7 @@
*/
package org.hyperledger.besu.consensus.common;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.plugin.data.Address;
import org.hyperledger.besu.plugin.data.BlockHeader;
@ -28,15 +28,13 @@ public class PoaQueryServiceImpl implements PoaQueryService, PoAMetricsService {
private final BlockInterface blockInterface;
private final Blockchain blockchain;
private final KeyPair localNodeKeypair;
private final NodeKey nodeKey;
public PoaQueryServiceImpl(
final BlockInterface blockInterface,
final Blockchain blockchain,
final KeyPair localNodeKeypair) {
final BlockInterface blockInterface, final Blockchain blockchain, final NodeKey nodeKey) {
this.blockInterface = blockInterface;
this.blockchain = blockchain;
this.localNodeKeypair = localNodeKeypair;
this.nodeKey = nodeKey;
}
@Override
@ -55,6 +53,6 @@ public class PoaQueryServiceImpl implements PoaQueryService, PoAMetricsService {
@Override
public Address getLocalSignerAddress() {
return org.hyperledger.besu.ethereum.core.Address.extract(localNodeKeypair.getPublicKey());
return org.hyperledger.besu.ethereum.core.Address.extract(nodeKey.getPublicKey());
}
}

@ -22,8 +22,7 @@ import org.hyperledger.besu.consensus.ibft.payload.CommitPayload;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.consensus.ibft.payload.SignedData;
import org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts;
import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.core.Block;
@ -33,16 +32,15 @@ import java.util.stream.Collectors;
public class IntegrationTestHelpers {
public static SignedData<CommitPayload> createSignedCommitPayload(
final ConsensusRoundIdentifier roundId, final Block block, final KeyPair signingKeyPair) {
final ConsensusRoundIdentifier roundId, final Block block, final NodeKey nodeKey) {
final IbftExtraData extraData = IbftExtraData.decode(block.getHeader());
final Signature commitSeal =
SECP256K1.sign(
IbftBlockHashing.calculateDataHashForCommittedSeal(block.getHeader(), extraData),
signingKeyPair);
nodeKey.sign(
IbftBlockHashing.calculateDataHashForCommittedSeal(block.getHeader(), extraData));
final MessageFactory messageFactory = new MessageFactory(signingKeyPair);
final MessageFactory messageFactory = new MessageFactory(nodeKey);
return messageFactory.createCommit(roundId, block.getHash(), commitSeal).getSignedPayload();
}

@ -14,7 +14,8 @@
*/
package org.hyperledger.besu.consensus.ibft.support;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.Util;
@ -53,9 +54,9 @@ public class NetworkLayout {
final TreeMap<Address, NodeParams> addressKeyMap = new TreeMap<>();
for (int i = 0; i < validatorCount; i++) {
final KeyPair newKeyPair = KeyPair.generate();
final Address nodeAddress = Util.publicKeyToAddress(newKeyPair.getPublicKey());
addressKeyMap.put(nodeAddress, new NodeParams(nodeAddress, newKeyPair));
final NodeKey newNodeKey = BouncyCastleNodeKey.generate();
final Address nodeAddress = Util.publicKeyToAddress(newNodeKey.getPublicKey());
addressKeyMap.put(nodeAddress, new NodeParams(nodeAddress, newNodeKey));
}
return addressKeyMap;

@ -14,23 +14,23 @@
*/
package org.hyperledger.besu.consensus.ibft.support;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.core.Address;
public class NodeParams {
private final Address address;
private final KeyPair nodeKeys;
private final NodeKey nodeKey;
public NodeParams(final Address address, final KeyPair nodeKeys) {
public NodeParams(final Address address, final NodeKey nodeKey) {
this.address = address;
this.nodeKeys = nodeKeys;
this.nodeKey = nodeKey;
}
public Address getAddress() {
return address;
}
public KeyPair getNodeKeyPair() {
return nodeKeys;
public NodeKey getNodeKey() {
return nodeKey;
}
}

@ -103,7 +103,7 @@ public class TestContext {
}
public NodeParams getLocalNodeParams() {
return new NodeParams(finalState.getLocalAddress(), finalState.getNodeKeys());
return new NodeParams(finalState.getLocalAddress(), finalState.getNodeKey());
}
public long getCurrentChainHeight() {

@ -50,7 +50,7 @@ import org.hyperledger.besu.consensus.ibft.statemachine.IbftController;
import org.hyperledger.besu.consensus.ibft.statemachine.IbftFinalState;
import org.hyperledger.besu.consensus.ibft.statemachine.IbftRoundFactory;
import org.hyperledger.besu.consensus.ibft.validation.MessageValidatorFactory;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.MinedBlockObserver;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
@ -175,8 +175,6 @@ public class TestContextBuilder {
final MutableBlockchain blockChain =
createInMemoryBlockchain(genesisBlock, IbftBlockHeaderFunctions.forOnChainBlock());
final KeyPair nodeKeys = networkNodes.getLocalNode().getNodeKeyPair();
// Use a stubbed version of the multicaster, to prevent creating PeerConnections etc.
final StubValidatorMulticaster multicaster = new StubValidatorMulticaster();
final UniqueMessageMulticaster uniqueMulticaster =
@ -190,7 +188,7 @@ public class TestContextBuilder {
createControllerAndFinalState(
blockChain,
multicaster,
nodeKeys,
networkNodes.getLocalNode().getNodeKey(),
clock,
ibftEventQueue,
gossiper,
@ -207,7 +205,7 @@ public class TestContextBuilder {
nodeParams ->
new ValidatorPeer(
nodeParams,
new MessageFactory(nodeParams.getNodeKeyPair()),
new MessageFactory(nodeParams.getNodeKey()),
controllerAndState.getEventMultiplexer()),
(u, v) -> {
throw new IllegalStateException(String.format("Duplicate key %s", u));
@ -256,7 +254,7 @@ public class TestContextBuilder {
private static ControllerAndState createControllerAndFinalState(
final MutableBlockchain blockChain,
final StubValidatorMulticaster multicaster,
final KeyPair nodeKeys,
final NodeKey nodeKey,
final Clock clock,
final IbftEventQueue ibftEventQueue,
final Gossiper gossiper,
@ -309,7 +307,7 @@ public class TestContextBuilder {
protocolContext,
protocolSchedule,
miningParams,
Util.publicKeyToAddress(nodeKeys.getPublicKey()));
Util.publicKeyToAddress(nodeKey.getPublicKey()));
final ProposerSelector proposerSelector =
new ProposerSelector(blockChain, blockInterface, true, voteTallyCache);
@ -318,15 +316,15 @@ public class TestContextBuilder {
final IbftFinalState finalState =
new IbftFinalState(
protocolContext.getConsensusState().getVoteTallyCache(),
nodeKeys,
Util.publicKeyToAddress(nodeKeys.getPublicKey()),
nodeKey,
Util.publicKeyToAddress(nodeKey.getPublicKey()),
proposerSelector,
multicaster,
new RoundTimer(ibftEventQueue, ROUND_TIMER_SEC * 1000, ibftExecutors),
new BlockTimer(
ibftEventQueue, BLOCK_TIMER_SEC * 1000, ibftExecutors, TestClock.fixed()),
blockCreatorFactory,
new MessageFactory(nodeKeys),
new MessageFactory(nodeKey),
clock);
final MessageValidatorFactory messageValidatorFactory =

@ -30,8 +30,7 @@ import org.hyperledger.besu.consensus.ibft.messagewrappers.RoundChange;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.consensus.ibft.payload.RoundChangeCertificate;
import org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts;
import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.Block;
@ -51,7 +50,7 @@ import org.apache.tuweni.bytes.Bytes;
public class ValidatorPeer {
private final Address nodeAddress;
private final KeyPair nodeKeys;
private final NodeKey nodeKey;
private final MessageFactory messageFactory;
private final PeerConnection peerConnection;
private final List<MessageData> receivedMessages = Lists.newArrayList();
@ -63,10 +62,10 @@ public class ValidatorPeer {
final NodeParams nodeParams,
final MessageFactory messageFactory,
final EventMultiplexer localEventMultiplexer) {
this.nodeKeys = nodeParams.getNodeKeyPair();
this.nodeKey = nodeParams.getNodeKey();
this.nodeAddress = nodeParams.getAddress();
this.messageFactory = messageFactory;
final Bytes nodeId = nodeKeys.getPublicKey().getEncodedBytes();
final Bytes nodeId = nodeKey.getPublicKey().getEncodedBytes();
this.peerConnection = StubbedPeerConnection.create(nodeId);
this.localEventMultiplexer = localEventMultiplexer;
}
@ -75,8 +74,8 @@ public class ValidatorPeer {
return nodeAddress;
}
public KeyPair getNodeKeys() {
return nodeKeys;
public NodeKey getnodeKey() {
return nodeKey;
}
public PeerConnection getPeerConnection() {
@ -97,11 +96,11 @@ public class ValidatorPeer {
}
public Signature getBlockSignature(final Hash digest) {
return SECP256K1.sign(digest, nodeKeys);
return nodeKey.sign(digest);
}
public Commit injectCommit(final ConsensusRoundIdentifier rId, final Hash digest) {
final Signature commitSeal = SECP256K1.sign(digest, nodeKeys);
final Signature commitSeal = nodeKey.sign(digest);
return injectCommit(rId, digest, commitSeal);
}
@ -153,10 +152,6 @@ public class ValidatorPeer {
return messageFactory;
}
public KeyPair getNodeKeyPair() {
return nodeKeys;
}
public void updateEstimatedChainHeight(final long estimatedChainHeight) {
this.estimatedChainHeight = estimatedChainHeight;
}

@ -99,9 +99,7 @@ public class FutureHeightTest {
final Commit expectedCommitMessage =
new Commit(
createSignedCommitPayload(
futureHeightRoundId,
futureHeightBlock,
context.getLocalNodeParams().getNodeKeyPair()));
futureHeightRoundId, futureHeightBlock, context.getLocalNodeParams().getNodeKey()));
peers.verifyMessagesReceived(expectedPrepareMessage, expectedCommitMessage);
assertThat(context.getCurrentChainHeight()).isEqualTo(2);
@ -157,7 +155,7 @@ public class FutureHeightTest {
final Commit expectedCommitMessage =
new Commit(
createSignedCommitPayload(
roundId, currentHeightBlock, context.getLocalNodeParams().getNodeKeyPair()));
roundId, currentHeightBlock, context.getLocalNodeParams().getNodeKey()));
peers.verifyMessagesReceived(expectedCommitMessage);
}
@ -214,9 +212,7 @@ public class FutureHeightTest {
final Commit expectedCommitMessage =
new Commit(
createSignedCommitPayload(
futureHeightRoundId,
futureHeightBlock,
context.getLocalNodeParams().getNodeKeyPair()));
futureHeightRoundId, futureHeightBlock, context.getLocalNodeParams().getNodeKey()));
// Assert ONLY a prepare message was received, not any commits (i.e. futureHeightRoundId
// messages have not been used.

@ -25,7 +25,6 @@ import org.hyperledger.besu.consensus.ibft.payload.RoundChangeCertificate;
import org.hyperledger.besu.consensus.ibft.support.RoundSpecificPeers;
import org.hyperledger.besu.consensus.ibft.support.TestContext;
import org.hyperledger.besu.consensus.ibft.support.TestContextBuilder;
import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.ethereum.core.Block;
import java.time.Clock;
@ -107,7 +106,7 @@ public class FutureRoundTest {
localNodeMessageFactory.createCommit(
futureRoundId,
futureBlock.getHash(),
SECP256K1.sign(futureBlock.getHash(), context.getLocalNodeParams().getNodeKeyPair()));
context.getLocalNodeParams().getNodeKey().sign(futureBlock.getHash()));
peers.verifyMessagesReceived(expectedCommit);
// requires 1 more commit and the blockchain will progress

@ -31,7 +31,7 @@ import org.hyperledger.besu.consensus.ibft.support.RoundSpecificPeers;
import org.hyperledger.besu.consensus.ibft.support.TestContext;
import org.hyperledger.besu.consensus.ibft.support.TestContextBuilder;
import org.hyperledger.besu.consensus.ibft.support.ValidatorPeer;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.ethereum.core.Block;
import java.time.Clock;
@ -118,8 +118,7 @@ public class GossipTest {
@Test
public void messageWithUnknownValidatorIsNotGossiped() {
final KeyPair unknownKeyPair = KeyPair.generate();
final MessageFactory unknownMsgFactory = new MessageFactory(unknownKeyPair);
final MessageFactory unknownMsgFactory = new MessageFactory(BouncyCastleNodeKey.generate());
final Proposal unknownProposal =
unknownMsgFactory.createProposal(roundId, block, Optional.empty());

@ -72,7 +72,7 @@ public class LocalNodeIsProposerTest {
expectedTxCommit =
new Commit(
createSignedCommitPayload(
roundId, expectedProposedBlock, context.getLocalNodeParams().getNodeKeyPair()));
roundId, expectedProposedBlock, context.getLocalNodeParams().getNodeKey()));
// Trigger "block timer" to send proposal.
context.getController().handleBlockTimerExpiry(new BlockTimerExpiry(roundId));

@ -57,7 +57,7 @@ public class LocalNodeNotProposerTest {
expectedTxCommit =
new Commit(
createSignedCommitPayload(
roundId, blockToPropose, context.getLocalNodeParams().getNodeKeyPair()));
roundId, blockToPropose, context.getLocalNodeParams().getNodeKey()));
}
@Test

@ -171,7 +171,7 @@ public class ReceivedFutureProposalTest {
final Commit expectedCommit =
new Commit(
IntegrationTestHelpers.createSignedCommitPayload(
nextRoundId, reproposedBlock, context.getLocalNodeParams().getNodeKeyPair()));
nextRoundId, reproposedBlock, context.getLocalNodeParams().getNodeKey()));
peers.verifyMessagesReceived(expectedCommit);
}

@ -27,8 +27,8 @@ import org.hyperledger.besu.consensus.ibft.support.RoundSpecificPeers;
import org.hyperledger.besu.consensus.ibft.support.TestContext;
import org.hyperledger.besu.consensus.ibft.support.TestContextBuilder;
import org.hyperledger.besu.consensus.ibft.support.ValidatorPeer;
import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.Hash;
@ -75,7 +75,7 @@ public class SpuriousBehaviourTest {
expectedCommit =
new Commit(
createSignedCommitPayload(
roundId, proposedBlock, context.getLocalNodeParams().getNodeKeyPair()));
roundId, proposedBlock, context.getLocalNodeParams().getNodeKey()));
}
@Test
@ -98,14 +98,15 @@ public class SpuriousBehaviourTest {
@Test
public void nonValidatorsCannotTriggerResponses() {
final KeyPair nonValidatorKeys = KeyPair.generate();
final NodeKey nonValidatorNodeKey = BouncyCastleNodeKey.generate();
final NodeParams nonValidatorParams =
new NodeParams(Util.publicKeyToAddress(nonValidatorKeys.getPublicKey()), nonValidatorKeys);
new NodeParams(
Util.publicKeyToAddress(nonValidatorNodeKey.getPublicKey()), nonValidatorNodeKey);
final ValidatorPeer nonvalidator =
new ValidatorPeer(
nonValidatorParams,
new MessageFactory(nonValidatorParams.getNodeKeyPair()),
new MessageFactory(nonValidatorParams.getNodeKey()),
context.getEventMultiplexer());
nonvalidator.injectProposal(new ConsensusRoundIdentifier(1, 0), proposedBlock);
@ -144,7 +145,7 @@ public class SpuriousBehaviourTest {
// nonProposer-2 will generate an invalid seal
final ValidatorPeer badSealPeer = peers.getNonProposing(2);
final Signature illegalSeal = SECP256K1.sign(Hash.ZERO, badSealPeer.getNodeKeys());
final Signature illegalSeal = badSealPeer.getnodeKey().sign(Hash.ZERO);
badSealPeer.injectCommit(roundId, proposedBlock.getHash(), illegalSeal);
assertThat(context.getCurrentChainHeight()).isEqualTo(0);

@ -20,8 +20,7 @@ import org.hyperledger.besu.consensus.ibft.messagewrappers.Prepare;
import org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal;
import org.hyperledger.besu.consensus.ibft.messagewrappers.RoundChange;
import org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts;
import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.Hash;
@ -33,10 +32,10 @@ import org.apache.tuweni.bytes.Bytes;
public class MessageFactory {
private final KeyPair validatorKeyPair;
private final NodeKey nodeKey;
public MessageFactory(final KeyPair validatorKeyPair) {
this.validatorKeyPair = validatorKeyPair;
public MessageFactory(final NodeKey nodeKey) {
this.nodeKey = nodeKey;
}
public Proposal createProposal(
@ -79,10 +78,9 @@ public class MessageFactory {
}
private <M extends Payload> SignedData<M> createSignedMessage(final M payload) {
final Signature signature = sign(payload, validatorKeyPair);
final Signature signature = nodeKey.sign(hashForSignature(payload));
return new SignedData<>(
payload, Util.publicKeyToAddress(validatorKeyPair.getPublicKey()), signature);
return new SignedData<>(payload, Util.publicKeyToAddress(nodeKey.getPublicKey()), signature);
}
public static Hash hashForSignature(final Payload unsignedMessageData) {
@ -90,8 +88,4 @@ public class MessageFactory {
Bytes.concatenate(
Bytes.of(unsignedMessageData.getMessageType()), unsignedMessageData.encoded()));
}
private static Signature sign(final Payload unsignedMessageData, final KeyPair nodeKeys) {
return SECP256K1.sign(hashForSignature(unsignedMessageData), nodeKeys);
}
}

@ -18,7 +18,7 @@ import org.hyperledger.besu.consensus.common.BlockInterface;
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.crypto.NodeKey;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Hash;
@ -33,8 +33,8 @@ import org.apache.tuweni.bytes.Bytes32;
public class IbftQueryServiceImpl extends PoaQueryServiceImpl implements IbftQueryService {
public IbftQueryServiceImpl(
final BlockInterface blockInterface, final Blockchain blockchain, final KeyPair keyPair) {
super(blockInterface, blockchain, keyPair);
final BlockInterface blockInterface, final Blockchain blockchain, final NodeKey nodeKey) {
super(blockInterface, blockchain, nodeKey);
}
@Override

@ -24,7 +24,7 @@ import org.hyperledger.besu.consensus.ibft.blockcreation.ProposerSelector;
import org.hyperledger.besu.consensus.ibft.network.IbftMessageTransmitter;
import org.hyperledger.besu.consensus.ibft.network.ValidatorMulticaster;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.core.Address;
import java.time.Clock;
@ -33,7 +33,7 @@ import java.util.Collection;
/** This is the full data set, or context, required for many of the aspects of the IBFT workflow. */
public class IbftFinalState {
private final VoteTallyCache voteTallyCache;
private final KeyPair nodeKeys;
private final NodeKey nodeKey;
private final Address localAddress;
private final ProposerSelector proposerSelector;
private final RoundTimer roundTimer;
@ -45,7 +45,7 @@ public class IbftFinalState {
public IbftFinalState(
final VoteTallyCache voteTallyCache,
final KeyPair nodeKeys,
final NodeKey nodeKey,
final Address localAddress,
final ProposerSelector proposerSelector,
final ValidatorMulticaster validatorMulticaster,
@ -55,7 +55,7 @@ public class IbftFinalState {
final MessageFactory messageFactory,
final Clock clock) {
this.voteTallyCache = voteTallyCache;
this.nodeKeys = nodeKeys;
this.nodeKey = nodeKey;
this.localAddress = localAddress;
this.proposerSelector = proposerSelector;
this.roundTimer = roundTimer;
@ -74,8 +74,8 @@ public class IbftFinalState {
return voteTallyCache.getVoteTallyAtHead().getValidators();
}
public KeyPair getNodeKeys() {
return nodeKeys;
public NodeKey getNodeKey() {
return nodeKey;
}
public Address getLocalAddress() {

@ -29,8 +29,7 @@ import org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal;
import org.hyperledger.besu.consensus.ibft.network.IbftMessageTransmitter;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.consensus.ibft.payload.RoundChangeCertificate;
import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.MinedBlockObserver;
@ -55,7 +54,7 @@ public class IbftRound {
private final IbftBlockCreator blockCreator;
private final ProtocolContext<IbftContext> protocolContext;
private final BlockImporter<IbftContext> blockImporter;
private final KeyPair nodeKeys;
private final NodeKey nodeKey;
private final MessageFactory messageFactory; // used only to create stored local msgs
private final IbftMessageTransmitter transmitter;
@ -65,7 +64,7 @@ public class IbftRound {
final ProtocolContext<IbftContext> protocolContext,
final BlockImporter<IbftContext> blockImporter,
final Subscribers<MinedBlockObserver> observers,
final KeyPair nodeKeys,
final NodeKey nodeKey,
final MessageFactory messageFactory,
final IbftMessageTransmitter transmitter,
final RoundTimer roundTimer) {
@ -74,7 +73,7 @@ public class IbftRound {
this.protocolContext = protocolContext;
this.blockImporter = blockImporter;
this.observers = observers;
this.nodeKeys = nodeKeys;
this.nodeKey = nodeKey;
this.messageFactory = messageFactory;
this.transmitter = transmitter;
@ -229,7 +228,7 @@ public class IbftRound {
final IbftExtraData extraData = IbftExtraData.decode(proposedHeader);
final Hash commitHash =
IbftBlockHashing.calculateDataHashForCommittedSeal(proposedHeader, extraData);
return SECP256K1.sign(commitHash, nodeKeys);
return nodeKey.sign(commitHash);
}
private void notifyNewBlockListeners(final Block block) {

@ -73,7 +73,7 @@ public class IbftRoundFactory {
protocolContext,
protocolSchedule.getByBlockNumber(roundIdentifier.getSequenceNumber()).getBlockImporter(),
minedBlockObservers,
finalState.getNodeKeys(),
finalState.getNodeKey(),
finalState.getMessageFactory(),
finalState.getTransmitter(),
finalState.getRoundTimer());

@ -17,7 +17,8 @@ package org.hyperledger.besu.consensus.ibft;
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Java6Assertions.assertThat;
import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.SECP256K1.PrivateKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
@ -42,7 +43,7 @@ import org.junit.Test;
public class IbftBlockHashingTest {
private static final List<KeyPair> COMMITTERS_KEY_PAIRS = committersKeyPairs();
private static final List<NodeKey> COMMITTERS_NODE_KEYS = committersNodeKeys();
private static final List<Address> VALIDATORS =
Arrays.asList(Address.fromHexString("1"), Address.fromHexString("2"));
private static final Optional<Vote> VOTE = Optional.of(Vote.authVote(Address.fromHexString("3")));
@ -65,8 +66,8 @@ public class IbftBlockHashingTest {
HEADER_TO_BE_HASHED, IbftExtraData.decode(HEADER_TO_BE_HASHED));
List<Address> expectedCommitterAddresses =
COMMITTERS_KEY_PAIRS.stream()
.map(keyPair -> Util.publicKeyToAddress(keyPair.getPublicKey()))
COMMITTERS_NODE_KEYS.stream()
.map(nodeKey -> Util.publicKeyToAddress(nodeKey.getPublicKey()))
.collect(Collectors.toList());
assertThat(actualCommitterAddresses).isEqualTo(expectedCommitterAddresses);
@ -81,8 +82,8 @@ public class IbftBlockHashingTest {
BlockHeaderBuilder builder = setHeaderFieldsExceptForExtraData();
List<Signature> commitSeals =
COMMITTERS_KEY_PAIRS.stream()
.map(keyPair -> SECP256K1.sign(dataHahsForCommittedSeal, keyPair))
COMMITTERS_NODE_KEYS.stream()
.map(nodeKey -> nodeKey.sign(dataHahsForCommittedSeal))
.collect(Collectors.toList());
IbftExtraData extraDataWithCommitSeals =
@ -93,9 +94,12 @@ public class IbftBlockHashingTest {
assertThat(actualHeader).isEqualTo(HEADER_TO_BE_HASHED);
}
private static List<KeyPair> committersKeyPairs() {
private static List<NodeKey> committersNodeKeys() {
return IntStream.rangeClosed(1, 4)
.mapToObj(i -> KeyPair.create(PrivateKey.create(UInt256.valueOf(i).toBytes())))
.mapToObj(
i ->
new BouncyCastleNodeKey(
(KeyPair.create(PrivateKey.create(UInt256.valueOf(i).toBytes())))))
.collect(Collectors.toList());
}
@ -152,10 +156,8 @@ public class IbftBlockHashingTest {
builder.buildBlockHeader().writeTo(rlpForHeaderFroCommittersSigning);
List<Signature> commitSeals =
COMMITTERS_KEY_PAIRS.stream()
.map(
keyPair ->
SECP256K1.sign(Hash.hash(rlpForHeaderFroCommittersSigning.encoded()), keyPair))
COMMITTERS_NODE_KEYS.stream()
.map(nodeKey -> nodeKey.sign(Hash.hash(rlpForHeaderFroCommittersSigning.encoded())))
.collect(Collectors.toList());
IbftExtraData extraDataWithCommitSeals =

@ -19,7 +19,8 @@ import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hyperledger.besu.consensus.ibft.IbftContextBuilder.setupContextWithValidators;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.BlockHeader;
@ -45,15 +46,15 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
@Test
public void ibftValidateHeaderPasses() {
final KeyPair proposerKeyPair = KeyPair.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
final NodeKey proposerNodeKey = BouncyCastleNodeKey.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
final List<Address> validators = singletonList(proposerAddress);
final BlockHeader parentHeader =
getPresetHeaderBuilder(1, proposerKeyPair, validators, null).buildHeader();
getPresetHeaderBuilder(1, proposerNodeKey, validators, null).buildHeader();
final BlockHeader blockHeader =
getPresetHeaderBuilder(2, proposerKeyPair, validators, parentHeader).buildHeader();
getPresetHeaderBuilder(2, proposerNodeKey, validators, parentHeader).buildHeader();
final BlockHeaderValidator<IbftContext> validator =
IbftBlockHeaderValidationRulesetFactory.ibftBlockHeaderValidator(5);
@ -66,15 +67,15 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
@Test
public void ibftValidateHeaderFailsOnExtraData() {
final KeyPair proposerKeyPair = KeyPair.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
final NodeKey proposerNodeKey = BouncyCastleNodeKey.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
final List<Address> validators = singletonList(proposerAddress);
final BlockHeader parentHeader =
getPresetHeaderBuilder(1, proposerKeyPair, validators, null).buildHeader();
getPresetHeaderBuilder(1, proposerNodeKey, validators, null).buildHeader();
final BlockHeader blockHeader =
getPresetHeaderBuilder(2, proposerKeyPair, emptyList(), parentHeader).buildHeader();
getPresetHeaderBuilder(2, proposerNodeKey, emptyList(), parentHeader).buildHeader();
final BlockHeaderValidator<IbftContext> validator =
IbftBlockHeaderValidationRulesetFactory.ibftBlockHeaderValidator(5);
@ -87,17 +88,18 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
@Test
public void ibftValidateHeaderFailsOnCoinbaseData() {
final KeyPair proposerKeyPair = KeyPair.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
final NodeKey proposerNodeKey = BouncyCastleNodeKey.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
final Address nonProposerAddress = Util.publicKeyToAddress(KeyPair.generate().getPublicKey());
final Address nonProposerAddress =
Util.publicKeyToAddress(BouncyCastleNodeKey.generate().getPublicKey());
final List<Address> validators = singletonList(proposerAddress);
final BlockHeader parentHeader =
getPresetHeaderBuilder(1, proposerKeyPair, validators, null).buildHeader();
getPresetHeaderBuilder(1, proposerNodeKey, validators, null).buildHeader();
final BlockHeader blockHeader =
getPresetHeaderBuilder(2, proposerKeyPair, validators, parentHeader)
getPresetHeaderBuilder(2, proposerNodeKey, validators, parentHeader)
.coinbase(nonProposerAddress)
.buildHeader();
@ -112,15 +114,15 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
@Test
public void ibftValidateHeaderFailsOnNonce() {
final KeyPair proposerKeyPair = KeyPair.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
final NodeKey proposerNodeKey = BouncyCastleNodeKey.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
final List<Address> validators = singletonList(proposerAddress);
final BlockHeader parentHeader =
getPresetHeaderBuilder(1, proposerKeyPair, validators, null).buildHeader();
getPresetHeaderBuilder(1, proposerNodeKey, validators, null).buildHeader();
final BlockHeader blockHeader =
getPresetHeaderBuilder(2, proposerKeyPair, validators, parentHeader).nonce(3).buildHeader();
getPresetHeaderBuilder(2, proposerNodeKey, validators, parentHeader).nonce(3).buildHeader();
final BlockHeaderValidator<IbftContext> validator =
IbftBlockHeaderValidationRulesetFactory.ibftBlockHeaderValidator(5);
@ -133,15 +135,15 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
@Test
public void ibftValidateHeaderFailsOnTimestamp() {
final KeyPair proposerKeyPair = KeyPair.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
final NodeKey proposerNodeKey = BouncyCastleNodeKey.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
final List<Address> validators = singletonList(proposerAddress);
final BlockHeader parentHeader =
getPresetHeaderBuilder(1, proposerKeyPair, validators, null).buildHeader();
getPresetHeaderBuilder(1, proposerNodeKey, validators, null).buildHeader();
final BlockHeader blockHeader =
getPresetHeaderBuilder(2, proposerKeyPair, validators, parentHeader)
getPresetHeaderBuilder(2, proposerNodeKey, validators, parentHeader)
.timestamp(100)
.buildHeader();
@ -156,15 +158,15 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
@Test
public void ibftValidateHeaderFailsOnMixHash() {
final KeyPair proposerKeyPair = KeyPair.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
final NodeKey proposerNodeKey = BouncyCastleNodeKey.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
final List<Address> validators = singletonList(proposerAddress);
final BlockHeader parentHeader =
getPresetHeaderBuilder(1, proposerKeyPair, validators, null).buildHeader();
getPresetHeaderBuilder(1, proposerNodeKey, validators, null).buildHeader();
final BlockHeader blockHeader =
getPresetHeaderBuilder(2, proposerKeyPair, validators, parentHeader)
getPresetHeaderBuilder(2, proposerNodeKey, validators, parentHeader)
.mixHash(Hash.EMPTY_TRIE_HASH)
.buildHeader();
@ -179,15 +181,15 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
@Test
public void ibftValidateHeaderFailsOnOmmers() {
final KeyPair proposerKeyPair = KeyPair.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
final NodeKey proposerNodeKey = BouncyCastleNodeKey.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
final List<Address> validators = singletonList(proposerAddress);
final BlockHeader parentHeader =
getPresetHeaderBuilder(1, proposerKeyPair, validators, null).buildHeader();
getPresetHeaderBuilder(1, proposerNodeKey, validators, null).buildHeader();
final BlockHeader blockHeader =
getPresetHeaderBuilder(2, proposerKeyPair, validators, parentHeader)
getPresetHeaderBuilder(2, proposerNodeKey, validators, parentHeader)
.ommersHash(Hash.EMPTY_TRIE_HASH)
.buildHeader();
@ -202,15 +204,15 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
@Test
public void ibftValidateHeaderFailsOnDifficulty() {
final KeyPair proposerKeyPair = KeyPair.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
final NodeKey proposerNodeKey = BouncyCastleNodeKey.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
final List<Address> validators = singletonList(proposerAddress);
final BlockHeader parentHeader =
getPresetHeaderBuilder(1, proposerKeyPair, validators, null).buildHeader();
getPresetHeaderBuilder(1, proposerNodeKey, validators, null).buildHeader();
final BlockHeader blockHeader =
getPresetHeaderBuilder(2, proposerKeyPair, validators, parentHeader)
getPresetHeaderBuilder(2, proposerNodeKey, validators, parentHeader)
.difficulty(Difficulty.of(5))
.buildHeader();
@ -225,15 +227,15 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
@Test
public void ibftValidateHeaderFailsOnAncestor() {
final KeyPair proposerKeyPair = KeyPair.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
final NodeKey proposerNodeKey = BouncyCastleNodeKey.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
final List<Address> validators = singletonList(proposerAddress);
final BlockHeader parentHeader =
getPresetHeaderBuilder(1, proposerKeyPair, validators, null).buildHeader();
getPresetHeaderBuilder(1, proposerNodeKey, validators, null).buildHeader();
final BlockHeader blockHeader =
getPresetHeaderBuilder(2, proposerKeyPair, validators, null).buildHeader();
getPresetHeaderBuilder(2, proposerNodeKey, validators, null).buildHeader();
final BlockHeaderValidator<IbftContext> validator =
IbftBlockHeaderValidationRulesetFactory.ibftBlockHeaderValidator(5);
@ -246,15 +248,15 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
@Test
public void ibftValidateHeaderFailsOnGasUsage() {
final KeyPair proposerKeyPair = KeyPair.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
final NodeKey proposerNodeKey = BouncyCastleNodeKey.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
final List<Address> validators = singletonList(proposerAddress);
final BlockHeader parentHeader =
getPresetHeaderBuilder(1, proposerKeyPair, validators, null).buildHeader();
getPresetHeaderBuilder(1, proposerNodeKey, validators, null).buildHeader();
final BlockHeader blockHeader =
getPresetHeaderBuilder(2, proposerKeyPair, validators, parentHeader)
getPresetHeaderBuilder(2, proposerNodeKey, validators, parentHeader)
.gasLimit(5_000)
.gasUsed(6_000)
.buildHeader();
@ -270,15 +272,15 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
@Test
public void ibftValidateHeaderFailsOnGasLimitRange() {
final KeyPair proposerKeyPair = KeyPair.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
final NodeKey proposerNodeKey = BouncyCastleNodeKey.generate();
final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
final List<Address> validators = singletonList(proposerAddress);
final BlockHeader parentHeader =
getPresetHeaderBuilder(1, proposerKeyPair, validators, null).buildHeader();
getPresetHeaderBuilder(1, proposerNodeKey, validators, null).buildHeader();
final BlockHeader blockHeader =
getPresetHeaderBuilder(2, proposerKeyPair, validators, parentHeader)
getPresetHeaderBuilder(2, proposerNodeKey, validators, parentHeader)
.gasLimit(4999)
.buildHeader();
@ -293,7 +295,7 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
private BlockHeaderTestFixture getPresetHeaderBuilder(
final long number,
final KeyPair proposerKeyPair,
final NodeKey proposerNodeKey,
final List<Address> validators,
final BlockHeader parent) {
final BlockHeaderTestFixture builder = new BlockHeaderTestFixture();
@ -309,7 +311,7 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
builder.ommersHash(Hash.EMPTY_LIST_HASH);
builder.nonce(0);
builder.difficulty(Difficulty.ONE);
builder.coinbase(Util.publicKeyToAddress(proposerKeyPair.getPublicKey()));
builder.coinbase(Util.publicKeyToAddress(proposerNodeKey.getPublicKey()));
final IbftExtraData ibftExtraData =
IbftExtraDataFixture.createExtraData(
@ -317,7 +319,7 @@ public class IbftBlockHeaderValidationRulesetFactoryTest {
Bytes.wrap(new byte[IbftExtraData.EXTRA_VANITY_LENGTH]),
Optional.of(Vote.authVote(Address.fromHexString("1"))),
validators,
singletonList(proposerKeyPair),
singletonList(proposerNodeKey),
0xDEADBEEF);
builder.extraData(ibftExtraData.encode());

@ -16,8 +16,7 @@ package org.hyperledger.besu.consensus.ibft;
import static java.util.Collections.emptyList;
import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.BlockHeader;
@ -37,9 +36,9 @@ public class IbftExtraDataFixture {
final Bytes vanityData,
final Optional<Vote> vote,
final List<Address> validators,
final List<KeyPair> committerKeyPairs) {
final List<NodeKey> committerNodeKeys) {
return createExtraData(header, vanityData, vote, validators, committerKeyPairs, 0);
return createExtraData(header, vanityData, vote, validators, committerNodeKeys, 0);
}
public static IbftExtraData createExtraData(
@ -47,11 +46,11 @@ public class IbftExtraDataFixture {
final Bytes vanityData,
final Optional<Vote> vote,
final List<Address> validators,
final List<KeyPair> committerKeyPairs,
final List<NodeKey> committerNodeKeys,
final int roundNumber) {
return createExtraData(
header, vanityData, vote, validators, committerKeyPairs, roundNumber, false);
header, vanityData, vote, validators, committerNodeKeys, roundNumber, false);
}
public static IbftExtraData createExtraData(
@ -59,7 +58,7 @@ public class IbftExtraDataFixture {
final Bytes vanityData,
final Optional<Vote> vote,
final List<Address> validators,
final List<KeyPair> committerKeyPairs,
final List<NodeKey> committerNodeKeys,
final int baseRoundNumber,
final boolean useDifferentRoundNumbersForCommittedSeals) {
@ -69,7 +68,7 @@ public class IbftExtraDataFixture {
// if useDifferentRoundNumbersForCommittedSeals is true then each committed seal will be
// calculated for an extraData field with a different round number
List<Signature> commitSeals =
IntStream.range(0, committerKeyPairs.size())
IntStream.range(0, committerNodeKeys.size())
.mapToObj(
i -> {
final int round =
@ -89,7 +88,7 @@ public class IbftExtraDataFixture {
IbftBlockHashing.calculateDataHashForCommittedSeal(
header, extraDataForCommittedSealCalculation);
return SECP256K1.sign(headerHashForCommitters, committerKeyPairs.get(i));
return committerNodeKeys.get(i).sign(headerHashForCommitters);
})
.collect(Collectors.toList());

@ -22,7 +22,8 @@ import org.hyperledger.besu.consensus.ibft.messagedata.RoundChangeMessageData;
import org.hyperledger.besu.consensus.ibft.messagewrappers.IbftMessage;
import org.hyperledger.besu.consensus.ibft.network.MockPeerFactory;
import org.hyperledger.besu.consensus.ibft.network.ValidatorMulticaster;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.AddressHelpers;
import org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection;
@ -52,9 +53,9 @@ public class IbftGossipTest {
}
private <P extends IbftMessage<?>> void assertRebroadcastToAllExceptSignerAndSender(
final Function<KeyPair, P> createPayload, final Function<P, MessageData> createMessageData) {
final KeyPair keypair = KeyPair.generate();
final P payload = createPayload.apply(keypair);
final Function<NodeKey, P> createPayload, final Function<P, MessageData> createMessageData) {
final NodeKey nodeKey = BouncyCastleNodeKey.generate();
final P payload = createPayload.apply(nodeKey);
final MessageData messageData = createMessageData.apply(payload);
final Message message = new DefaultMessage(peerConnection, messageData);

@ -22,7 +22,8 @@ import org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.consensus.ibft.payload.PreparedCertificate;
import org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.Hash;
@ -85,7 +86,7 @@ public class IbftHelpersTest {
// sources, it is only responsible for determine which of the list or RoundChange messages
// contains the newest
// NOTE: This capability is tested as part of the NewRoundMessageValidationTests.
final KeyPair proposerKey = KeyPair.generate();
final NodeKey proposerKey = BouncyCastleNodeKey.generate();
final MessageFactory proposerMessageFactory = new MessageFactory(proposerKey);
final Block proposedBlock = mock(Block.class);
when(proposedBlock.getHash()).thenReturn(Hash.fromHexStringLenient("1"));
@ -137,7 +138,7 @@ public class IbftHelpersTest {
@Test
public void allRoundChangeHaveNoPreparedReturnsEmptyOptional() {
final KeyPair proposerKey = KeyPair.generate();
final NodeKey proposerKey = BouncyCastleNodeKey.generate();
final MessageFactory proposerMessageFactory = new MessageFactory(proposerKey);
final ConsensusRoundIdentifier roundIdentifier = new ConsensusRoundIdentifier(1, 4);

@ -21,7 +21,7 @@ import org.hyperledger.besu.consensus.ibft.messagewrappers.Prepare;
import org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal;
import org.hyperledger.besu.consensus.ibft.messagewrappers.RoundChange;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.AddressHelpers;
@ -63,13 +63,13 @@ public class TestHelpers {
return new BlockDataGenerator().block(blockOptions);
}
public static Proposal createSignedProposalPayload(final KeyPair signerKeys) {
return createSignedProposalPayloadWithRound(signerKeys, 0xFEDCBA98);
public static Proposal createSignedProposalPayload(final NodeKey signerNodeKey) {
return createSignedProposalPayloadWithRound(signerNodeKey, 0xFEDCBA98);
}
public static Proposal createSignedProposalPayloadWithRound(
final KeyPair signerKeys, final int round) {
final MessageFactory messageFactory = new MessageFactory(signerKeys);
final NodeKey signerNodeKey, final int round) {
final MessageFactory messageFactory = new MessageFactory(signerNodeKey);
final ConsensusRoundIdentifier roundIdentifier =
new ConsensusRoundIdentifier(0x1234567890ABCDEFL, round);
final Block block =
@ -77,14 +77,14 @@ public class TestHelpers {
return messageFactory.createProposal(roundIdentifier, block, Optional.empty());
}
public static Prepare createSignedPreparePayload(final KeyPair signerKeys) {
public static Prepare createSignedPreparePayload(final NodeKey signerKeys) {
final MessageFactory messageFactory = new MessageFactory(signerKeys);
final ConsensusRoundIdentifier roundIdentifier =
new ConsensusRoundIdentifier(0x1234567890ABCDEFL, 0xFEDCBA98);
return messageFactory.createPrepare(roundIdentifier, Hash.fromHexStringLenient("0"));
}
public static Commit createSignedCommitPayload(final KeyPair signerKeys) {
public static Commit createSignedCommitPayload(final NodeKey signerKeys) {
final MessageFactory messageFactory = new MessageFactory(signerKeys);
final ConsensusRoundIdentifier roundIdentifier =
new ConsensusRoundIdentifier(0x1234567890ABCDEFL, 0xFEDCBA98);
@ -94,7 +94,7 @@ public class TestHelpers {
Signature.create(BigInteger.ONE, BigInteger.TEN, (byte) 0));
}
public static RoundChange createSignedRoundChangePayload(final KeyPair signerKeys) {
public static RoundChange createSignedRoundChangePayload(final NodeKey signerKeys) {
final MessageFactory messageFactory = new MessageFactory(signerKeys);
final ConsensusRoundIdentifier roundIdentifier =
new ConsensusRoundIdentifier(0x1234567890ABCDEFL, 0xFEDCBA98);

@ -17,7 +17,7 @@ package org.hyperledger.besu.consensus.ibft.headervalidationrules;
import org.hyperledger.besu.consensus.ibft.IbftExtraData;
import org.hyperledger.besu.consensus.ibft.IbftExtraDataFixture;
import org.hyperledger.besu.consensus.ibft.Vote;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
@ -31,7 +31,7 @@ public class HeaderValidationTestHelpers {
public static BlockHeader createProposedBlockHeader(
final List<Address> validators,
final List<KeyPair> committerKeyPairs,
final List<NodeKey> committerNodeKeys,
final boolean useDifferentRoundNumbersForCommittedSeals) {
final int BASE_ROUND_NUMBER = 5;
final BlockHeaderTestFixture builder = new BlockHeaderTestFixture();
@ -45,7 +45,7 @@ public class HeaderValidationTestHelpers {
Bytes.wrap(new byte[IbftExtraData.EXTRA_VANITY_LENGTH]),
Optional.of(Vote.authVote(Address.fromHexString("1"))),
validators,
committerKeyPairs,
committerNodeKeys,
BASE_ROUND_NUMBER,
useDifferentRoundNumbersForCommittedSeals);

@ -21,7 +21,8 @@ import org.hyperledger.besu.consensus.ibft.IbftContext;
import org.hyperledger.besu.consensus.ibft.IbftExtraData;
import org.hyperledger.besu.consensus.ibft.IbftExtraDataFixture;
import org.hyperledger.besu.consensus.ibft.Vote;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.BlockHeader;
@ -39,13 +40,13 @@ import org.junit.Test;
public class IbftCoinbaseValidationRuleTest {
public static BlockHeader createProposedBlockHeader(
final KeyPair proposerKeyPair,
final NodeKey proposerNodeKey,
final List<Address> validators,
final List<KeyPair> committerKeyPairs) {
final List<NodeKey> committerNodeKeys) {
final BlockHeaderTestFixture builder = new BlockHeaderTestFixture();
builder.number(1); // must NOT be block 0, as that should not contain seals at all
builder.coinbase(Util.publicKeyToAddress(proposerKeyPair.getPublicKey()));
builder.coinbase(Util.publicKeyToAddress(proposerNodeKey.getPublicKey()));
final BlockHeader header = builder.buildHeader();
final IbftExtraData ibftExtraData =
@ -54,7 +55,7 @@ public class IbftCoinbaseValidationRuleTest {
Bytes.wrap(new byte[IbftExtraData.EXTRA_VANITY_LENGTH]),
Optional.of(Vote.authVote(Address.fromHexString("1"))),
validators,
committerKeyPairs);
committerNodeKeys);
builder.extraData(ibftExtraData.encode());
return builder.buildHeader();
@ -62,42 +63,42 @@ public class IbftCoinbaseValidationRuleTest {
@Test
public void proposerInValidatorListPassesValidation() {
final KeyPair proposerKeyPair = KeyPair.generate();
final NodeKey proposerNodeKey = BouncyCastleNodeKey.generate();
final Address proposerAddress =
Address.extract(Hash.hash(proposerKeyPair.getPublicKey().getEncodedBytes()));
Address.extract(Hash.hash(proposerNodeKey.getPublicKey().getEncodedBytes()));
final List<Address> validators = Lists.newArrayList(proposerAddress);
final List<KeyPair> committers = Lists.newArrayList(proposerKeyPair);
final List<NodeKey> committers = Lists.newArrayList(proposerNodeKey);
final ProtocolContext<IbftContext> context =
new ProtocolContext<>(null, null, setupContextWithValidators(validators));
final IbftCoinbaseValidationRule coinbaseValidationRule = new IbftCoinbaseValidationRule();
BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators, committers);
BlockHeader header = createProposedBlockHeader(proposerNodeKey, validators, committers);
assertThat(coinbaseValidationRule.validate(header, null, context)).isTrue();
}
@Test
public void proposerNotInValidatorListFailsValidation() {
final KeyPair proposerKeyPair = KeyPair.generate();
final NodeKey proposerNodeKey = BouncyCastleNodeKey.generate();
final KeyPair otherValidatorKeyPair = KeyPair.generate();
final NodeKey otherValidatorNodeKey = BouncyCastleNodeKey.generate();
final Address otherValidatorNodeAddress =
Address.extract(Hash.hash(otherValidatorKeyPair.getPublicKey().getEncodedBytes()));
Address.extract(Hash.hash(otherValidatorNodeKey.getPublicKey().getEncodedBytes()));
final List<Address> validators = Lists.newArrayList(otherValidatorNodeAddress);
final List<KeyPair> committers = Lists.newArrayList(otherValidatorKeyPair);
final List<NodeKey> committers = Lists.newArrayList(otherValidatorNodeKey);
final ProtocolContext<IbftContext> context =
new ProtocolContext<>(null, null, setupContextWithValidators(validators));
final IbftCoinbaseValidationRule coinbaseValidationRule = new IbftCoinbaseValidationRule();
BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators, committers);
BlockHeader header = createProposedBlockHeader(proposerNodeKey, validators, committers);
assertThat(coinbaseValidationRule.validate(header, null, context)).isFalse();
}

@ -22,7 +22,8 @@ import static org.hyperledger.besu.consensus.ibft.headervalidationrules.HeaderVa
import org.hyperledger.besu.consensus.ibft.IbftContext;
import org.hyperledger.besu.consensus.ibft.IbftExtraData;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.BlockHeader;
@ -44,28 +45,30 @@ public class IbftCommitSealsValidationRuleTest {
@Test
public void correctlyConstructedHeaderPassesValidation() {
final List<KeyPair> committerKeyPairs =
IntStream.range(0, 2).mapToObj(i -> KeyPair.generate()).collect(Collectors.toList());
final List<NodeKey> committerNodeKeys =
IntStream.range(0, 2)
.mapToObj(i -> BouncyCastleNodeKey.generate())
.collect(Collectors.toList());
final List<Address> committerAddresses =
committerKeyPairs.stream()
.map(keyPair -> Util.publicKeyToAddress(keyPair.getPublicKey()))
committerNodeKeys.stream()
.map(nodeKey -> Util.publicKeyToAddress(nodeKey.getPublicKey()))
.sorted()
.collect(Collectors.toList());
final ProtocolContext<IbftContext> context =
new ProtocolContext<>(null, null, setupContextWithValidators(committerAddresses));
BlockHeader header = createProposedBlockHeader(committerAddresses, committerKeyPairs, false);
BlockHeader header = createProposedBlockHeader(committerAddresses, committerNodeKeys, false);
assertThat(commitSealsValidationRule.validate(header, null, context)).isTrue();
}
@Test
public void insufficientCommitSealsFailsValidation() {
final KeyPair committerKeyPair = KeyPair.generate();
final NodeKey committerNodeKey = BouncyCastleNodeKey.generate();
final Address committerAddress =
Address.extract(Hash.hash(committerKeyPair.getPublicKey().getEncodedBytes()));
Address.extract(Hash.hash(committerNodeKey.getPublicKey().getEncodedBytes()));
final List<Address> validators = singletonList(committerAddress);
final ProtocolContext<IbftContext> context =
@ -82,16 +85,16 @@ public class IbftCommitSealsValidationRuleTest {
@Test
public void committerNotInValidatorListFailsValidation() {
final KeyPair committerKeyPair = KeyPair.generate();
final Address committerAddress = Util.publicKeyToAddress(committerKeyPair.getPublicKey());
final NodeKey committerNodeKey = BouncyCastleNodeKey.generate();
final Address committerAddress = Util.publicKeyToAddress(committerNodeKey.getPublicKey());
final List<Address> validators = singletonList(committerAddress);
// Insert an extraData block with committer seals.
final KeyPair nonValidatorKeyPair = KeyPair.generate();
final NodeKey nonValidatorNodeKey = BouncyCastleNodeKey.generate();
final BlockHeader header =
createProposedBlockHeader(validators, singletonList(nonValidatorKeyPair), false);
createProposedBlockHeader(validators, singletonList(nonValidatorNodeKey), false);
final ProtocolContext<IbftContext> context =
new ProtocolContext<>(null, null, setupContextWithValidators(validators));
@ -140,12 +143,12 @@ public class IbftCommitSealsValidationRuleTest {
@Test
public void headerContainsDuplicateSealsFailsValidation() {
final KeyPair committerKeyPair = KeyPair.generate();
final NodeKey committerNodeKey = BouncyCastleNodeKey.generate();
final List<Address> validators =
singletonList(Util.publicKeyToAddress(committerKeyPair.getPublicKey()));
singletonList(Util.publicKeyToAddress(committerNodeKey.getPublicKey()));
final BlockHeader header =
createProposedBlockHeader(
validators, Lists.newArrayList(committerKeyPair, committerKeyPair), false);
validators, Lists.newArrayList(committerNodeKey, committerNodeKey), false);
final ProtocolContext<IbftContext> context =
new ProtocolContext<>(null, null, setupContextWithValidators(validators));
@ -159,12 +162,12 @@ public class IbftCommitSealsValidationRuleTest {
final boolean useDifferentRoundNumbersForCommittedSeals) {
final List<Address> validators = Lists.newArrayList();
final List<KeyPair> committerKeys = Lists.newArrayList();
final List<NodeKey> committerKeys = Lists.newArrayList();
for (int i = 0; i < validatorCount; i++) { // need -1 to account for proposer
final KeyPair committerKeyPair = KeyPair.generate();
committerKeys.add(committerKeyPair);
validators.add(Address.extract(Hash.hash(committerKeyPair.getPublicKey().getEncodedBytes())));
final NodeKey committerNodeKey = BouncyCastleNodeKey.generate();
committerKeys.add(committerNodeKey);
validators.add(Address.extract(Hash.hash(committerNodeKey.getPublicKey().getEncodedBytes())));
}
Collections.sort(validators);

@ -130,19 +130,19 @@ public class IbftGetSignerMetricsTest {
final List<SignerMetricResult> signerMetricResultList = new ArrayList<>();
// sign a first block with keypairs number 1
final SignerMetricResult signerMetricResultFirstKeyPairs = generateBlock(startBlock);
signerMetricResultList.add(signerMetricResultFirstKeyPairs);
// sign a second block with keypairs number 2
final SignerMetricResult signerMetricResultSecondKeyPairs = generateBlock(startBlock + 1);
signerMetricResultList.add(signerMetricResultSecondKeyPairs);
// sign a third block with keypairs number 3
final SignerMetricResult signerMetricResultThirdKeyPairs = generateBlock(startBlock + 2);
signerMetricResultList.add(signerMetricResultThirdKeyPairs);
// sign the last block with the keypairs number 1
// sign a first block with nodekey number 1
final SignerMetricResult signerMetricResultFirstNodeKeys = generateBlock(startBlock);
signerMetricResultList.add(signerMetricResultFirstNodeKeys);
// sign a second block with nodekey number 2
final SignerMetricResult signerMetricResultSecondNodeKeys = generateBlock(startBlock + 1);
signerMetricResultList.add(signerMetricResultSecondNodeKeys);
// sign a third block with nodekey number 3
final SignerMetricResult signerMetricResultThirdNodeKeys = generateBlock(startBlock + 2);
signerMetricResultList.add(signerMetricResultThirdNodeKeys);
// sign the last block with the nodekey number 1
generateBlock(startBlock + 3);
signerMetricResultFirstKeyPairs.setLastProposedBlockNumber(startBlock + 3);
signerMetricResultFirstKeyPairs.incrementeNbBlock();
signerMetricResultFirstNodeKeys.setLastProposedBlockNumber(startBlock + 3);
signerMetricResultFirstNodeKeys.incrementeNbBlock();
final JsonRpcRequestContext request = requestWithParams();

@ -22,8 +22,8 @@ import org.hyperledger.besu.consensus.ibft.IbftBlockHashing;
import org.hyperledger.besu.consensus.ibft.IbftBlockHeaderFunctions;
import org.hyperledger.besu.consensus.ibft.IbftBlockInterface;
import org.hyperledger.besu.consensus.ibft.IbftExtraData;
import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.Address;
@ -51,12 +51,11 @@ import org.mockito.junit.MockitoJUnitRunner;
public class IbftQueryServiceImplTest {
@Mock private Blockchain blockchain;
@Mock private KeyPair keyPair;
private final List<KeyPair> validatorKeys =
Lists.newArrayList(KeyPair.generate(), KeyPair.generate());
private final List<NodeKey> validatorKeys =
Lists.newArrayList(BouncyCastleNodeKey.generate(), BouncyCastleNodeKey.generate());
private final List<KeyPair> signingKeys = Lists.newArrayList(validatorKeys.get(0));
private final List<NodeKey> signingKeys = Lists.newArrayList(validatorKeys.get(0));
private final int ROUND_NUMBER_IN_BLOCK = 5;
private IbftExtraData signedExtraData;
@ -88,10 +87,9 @@ public class IbftQueryServiceImplTest {
final Collection<Signature> validatorSignatures =
signingKeys.stream()
.map(
keyPair ->
SECP256K1.sign(
IbftBlockHashing.calculateDataHashForCommittedSeal(unsignedBlockHeader),
keyPair))
nodeKey ->
nodeKey.sign(
IbftBlockHashing.calculateDataHashForCommittedSeal(unsignedBlockHeader)))
.collect(Collectors.toList());
signedExtraData =
@ -109,7 +107,7 @@ public class IbftQueryServiceImplTest {
@Test
public void roundNumberFromBlockIsReturned() {
final IbftQueryService service =
new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain, keyPair);
new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain, null);
assertThat(service.getRoundNumberFrom(blockHeader)).isEqualTo(ROUND_NUMBER_IN_BLOCK);
}
@ -121,7 +119,7 @@ public class IbftQueryServiceImplTest {
when(blockchain.getBlockHeader(blockHeader.getHash())).thenReturn(Optional.empty());
final IbftQueryService service =
new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain, keyPair);
new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain, null);
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> service.getRoundNumberFrom(header));
}
@ -129,11 +127,11 @@ public class IbftQueryServiceImplTest {
@Test
public void getSignersReturnsAddressesOfSignersInBlock() {
final IbftQueryService service =
new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain, keyPair);
new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain, null);
final List<Address> signers =
signingKeys.stream()
.map(keyPair -> Util.publicKeyToAddress(keyPair.getPublicKey()))
.map(nodeKey -> Util.publicKeyToAddress(nodeKey.getPublicKey()))
.collect(Collectors.toList());
assertThat(service.getSignersFrom(blockHeader)).containsExactlyElementsOf(signers);
@ -146,7 +144,7 @@ public class IbftQueryServiceImplTest {
when(blockchain.getBlockHeader(blockHeader.getHash())).thenReturn(Optional.empty());
final IbftQueryService service =
new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain, keyPair);
new IbftQueryServiceImpl(new IbftBlockInterface(), blockchain, null);
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> service.getSignersFrom(header));
}

@ -47,7 +47,8 @@ import org.hyperledger.besu.consensus.ibft.payload.RoundChangeCertificate;
import org.hyperledger.besu.consensus.ibft.validation.FutureRoundProposalMessageValidator;
import org.hyperledger.besu.consensus.ibft.validation.MessageValidator;
import org.hyperledger.besu.consensus.ibft.validation.MessageValidatorFactory;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.Address;
@ -80,8 +81,8 @@ import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class IbftBlockHeightManagerTest {
private final KeyPair localNodeKeys = KeyPair.generate();
private final MessageFactory messageFactory = new MessageFactory(localNodeKeys);
private final NodeKey nodeKey = BouncyCastleNodeKey.generate();
private final MessageFactory messageFactory = new MessageFactory(nodeKey);
private final BlockHeaderTestFixture headerTestFixture = new BlockHeaderTestFixture();
@Mock private IbftFinalState finalState;
@ -118,9 +119,9 @@ public class IbftBlockHeightManagerTest {
@Before
public void setup() {
for (int i = 0; i < 3; i++) {
final KeyPair key = KeyPair.generate();
validators.add(Util.publicKeyToAddress(key.getPublicKey()));
validatorMessageFactory.add(new MessageFactory(key));
final BouncyCastleNodeKey nodeKey = BouncyCastleNodeKey.generate();
validators.add(Util.publicKeyToAddress(nodeKey.getPublicKey()));
validatorMessageFactory.add(new MessageFactory(nodeKey));
}
buildCreatedBlock();
@ -156,7 +157,7 @@ public class IbftBlockHeightManagerTest {
protocolContext,
blockImporter,
Subscribers.create(),
localNodeKeys,
nodeKey,
messageFactory,
messageTransmitter,
roundTimer);
@ -172,7 +173,7 @@ public class IbftBlockHeightManagerTest {
protocolContext,
blockImporter,
Subscribers.create(),
localNodeKeys,
nodeKey,
messageFactory,
messageTransmitter,
roundTimer);

@ -36,8 +36,8 @@ import org.hyperledger.besu.consensus.ibft.network.IbftMessageTransmitter;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.consensus.ibft.payload.RoundChangeCertificate;
import org.hyperledger.besu.consensus.ibft.validation.MessageValidator;
import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.MinedBlockObserver;
@ -67,9 +67,9 @@ import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class IbftRoundTest {
private final KeyPair localNodeKeys = KeyPair.generate();
private final NodeKey nodeKey = BouncyCastleNodeKey.generate();
private final ConsensusRoundIdentifier roundIdentifier = new ConsensusRoundIdentifier(1, 0);
private final MessageFactory messageFactory = new MessageFactory(localNodeKeys);
private final MessageFactory messageFactory = new MessageFactory(nodeKey);
private final Subscribers<MinedBlockObserver> subscribers = Subscribers.create();
private ProtocolContext<IbftContext> protocolContext;
@ -125,7 +125,7 @@ public class IbftRoundTest {
protocolContext,
blockImporter,
subscribers,
localNodeKeys,
nodeKey,
messageFactory,
transmitter,
roundTimer);
@ -142,7 +142,7 @@ public class IbftRoundTest {
protocolContext,
blockImporter,
subscribers,
localNodeKeys,
nodeKey,
messageFactory,
transmitter,
roundTimer);
@ -163,7 +163,7 @@ public class IbftRoundTest {
protocolContext,
blockImporter,
subscribers,
localNodeKeys,
nodeKey,
messageFactory,
transmitter,
roundTimer);
@ -185,7 +185,7 @@ public class IbftRoundTest {
protocolContext,
blockImporter,
subscribers,
localNodeKeys,
nodeKey,
messageFactory,
transmitter,
roundTimer);
@ -207,7 +207,7 @@ public class IbftRoundTest {
protocolContext,
blockImporter,
subscribers,
localNodeKeys,
nodeKey,
messageFactory,
transmitter,
roundTimer);
@ -215,7 +215,7 @@ public class IbftRoundTest {
final Hash commitSealHash =
IbftBlockHashing.calculateDataHashForCommittedSeal(
proposedBlock.getHeader(), proposedExtraData);
final Signature localCommitSeal = SECP256K1.sign(commitSealHash, localNodeKeys);
final Signature localCommitSeal = nodeKey.sign(commitSealHash);
// Receive Proposal Message
round.handleProposalMessage(
@ -250,7 +250,7 @@ public class IbftRoundTest {
protocolContext,
blockImporter,
subscribers,
localNodeKeys,
nodeKey,
messageFactory,
transmitter,
roundTimer);
@ -258,7 +258,7 @@ public class IbftRoundTest {
final Hash commitSealHash =
IbftBlockHashing.calculateDataHashForCommittedSeal(
proposedBlock.getHeader(), proposedExtraData);
final Signature localCommitSeal = SECP256K1.sign(commitSealHash, localNodeKeys);
final Signature localCommitSeal = nodeKey.sign(commitSealHash);
round.createAndSendProposalMessage(15);
verify(transmitter, never()).multicastCommit(any(), any(), any());
@ -286,7 +286,7 @@ public class IbftRoundTest {
protocolContext,
blockImporter,
subscribers,
localNodeKeys,
nodeKey,
messageFactory,
transmitter,
roundTimer);
@ -309,7 +309,7 @@ public class IbftRoundTest {
protocolContext,
blockImporter,
subscribers,
localNodeKeys,
nodeKey,
messageFactory,
transmitter,
roundTimer);
@ -355,7 +355,7 @@ public class IbftRoundTest {
protocolContext,
blockImporter,
subscribers,
localNodeKeys,
nodeKey,
messageFactory,
transmitter,
roundTimer);
@ -388,7 +388,7 @@ public class IbftRoundTest {
protocolContext,
blockImporter,
subscribers,
localNodeKeys,
nodeKey,
messageFactory,
transmitter,
roundTimer);
@ -408,7 +408,7 @@ public class IbftRoundTest {
protocolContext,
blockImporter,
subscribers,
localNodeKeys,
nodeKey,
messageFactory,
transmitter,
roundTimer);
@ -433,7 +433,7 @@ public class IbftRoundTest {
protocolContext,
blockImporter,
subscribers,
localNodeKeys,
nodeKey,
messageFactory,
transmitter,
roundTimer);

@ -22,7 +22,8 @@ import org.hyperledger.besu.consensus.ibft.ConsensusRoundIdentifier;
import org.hyperledger.besu.consensus.ibft.TestHelpers;
import org.hyperledger.besu.consensus.ibft.messagewrappers.RoundChange;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.core.Block;
import java.util.List;
@ -43,8 +44,8 @@ public class RoundChangeArtifactsTest {
@Before
public void setup() {
for (int i = 0; i < 4; i++) {
final KeyPair keyPair = KeyPair.generate();
final MessageFactory messageFactory = new MessageFactory(keyPair);
final NodeKey nodeKey = BouncyCastleNodeKey.generate();
final MessageFactory messageFactory = new MessageFactory(nodeKey);
messageFactories.add(messageFactory);
}
}

@ -31,7 +31,8 @@ import org.hyperledger.besu.consensus.ibft.validation.ProposalBlockConsistencyVa
import org.hyperledger.besu.consensus.ibft.validation.RoundChangeMessageValidator;
import org.hyperledger.besu.consensus.ibft.validation.RoundChangePayloadValidator;
import org.hyperledger.besu.consensus.ibft.validation.SignedDataValidator;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.BlockValidator;
import org.hyperledger.besu.ethereum.BlockValidator.BlockProcessingOutputs;
import org.hyperledger.besu.ethereum.core.Address;
@ -52,10 +53,10 @@ public class RoundChangeManagerTest {
private RoundChangeManager manager;
private final KeyPair proposerKey = KeyPair.generate();
private final KeyPair validator1Key = KeyPair.generate();
private final KeyPair validator2Key = KeyPair.generate();
private final KeyPair nonValidatorKey = KeyPair.generate();
private final NodeKey proposerKey = BouncyCastleNodeKey.generate();
private final NodeKey validator1Key = BouncyCastleNodeKey.generate();
private final NodeKey validator2Key = BouncyCastleNodeKey.generate();
private final NodeKey nonValidatorKey = BouncyCastleNodeKey.generate();
private final ConsensusRoundIdentifier ri1 = new ConsensusRoundIdentifier(2, 1);
private final ConsensusRoundIdentifier ri2 = new ConsensusRoundIdentifier(2, 2);
@ -111,15 +112,15 @@ public class RoundChangeManagerTest {
}
private RoundChange makeRoundChangeMessage(
final KeyPair key, final ConsensusRoundIdentifier round) {
final NodeKey key, final ConsensusRoundIdentifier round) {
final MessageFactory messageFactory = new MessageFactory(key);
return messageFactory.createRoundChange(round, Optional.empty());
}
private RoundChange makeRoundChangeMessageWithPreparedCert(
final KeyPair key,
final NodeKey key,
final ConsensusRoundIdentifier round,
final List<KeyPair> prepareProviders) {
final List<NodeKey> prepareProviders) {
Preconditions.checkArgument(!prepareProviders.contains(key));
final MessageFactory messageFactory = new MessageFactory(key);

@ -26,7 +26,8 @@ import org.hyperledger.besu.consensus.ibft.messagewrappers.Prepare;
import org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.consensus.ibft.validation.MessageValidator;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.Block;
@ -47,7 +48,7 @@ import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class RoundStateTest {
private final List<KeyPair> validatorKeys = Lists.newArrayList();
private final List<NodeKey> validatorKeys = Lists.newArrayList();
private final List<MessageFactory> validatorMessageFactories = Lists.newArrayList();
private final ConsensusRoundIdentifier roundIdentifier = new ConsensusRoundIdentifier(1, 1);
@ -60,10 +61,10 @@ public class RoundStateTest {
@Before
public void setup() {
for (int i = 0; i < 3; i++) {
final KeyPair newKeyPair = KeyPair.generate();
validatorKeys.add(newKeyPair);
validators.add(Util.publicKeyToAddress(newKeyPair.getPublicKey()));
validatorMessageFactories.add(new MessageFactory(newKeyPair));
final NodeKey newNodeKey = BouncyCastleNodeKey.generate();
validatorKeys.add(newNodeKey);
validators.add(Util.publicKeyToAddress(newNodeKey.getPublicKey()));
validatorMessageFactories.add(new MessageFactory(newNodeKey));
}
when(block.getHash()).thenReturn(Hash.fromHexStringLenient("1"));
}

@ -24,7 +24,7 @@ import org.hyperledger.besu.consensus.ibft.ConsensusRoundIdentifier;
import org.hyperledger.besu.consensus.ibft.TestHelpers;
import org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockHeader;
@ -38,8 +38,7 @@ import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class FutureRoundProposalMessageValidatorTest {
private final KeyPair proposerKey = KeyPair.generate();
private final MessageFactory messageFactoy = new MessageFactory(proposerKey);
private final MessageFactory messageFactoy = new MessageFactory(BouncyCastleNodeKey.generate());
private final ConsensusRoundIdentifier roundIdentifier = new ConsensusRoundIdentifier(1, 1);
private final Block proposedBlock = TestHelpers.createProposalBlock(emptyList(), roundIdentifier);

@ -31,8 +31,8 @@ import org.hyperledger.besu.consensus.ibft.messagewrappers.Prepare;
import org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.consensus.ibft.payload.RoundChangeCertificate;
import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.BlockValidator;
import org.hyperledger.besu.ethereum.BlockValidator.BlockProcessingOutputs;
import org.hyperledger.besu.ethereum.ProtocolContext;
@ -55,8 +55,8 @@ import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class MessageValidatorTest {
private final KeyPair keyPair = KeyPair.generate();
private final MessageFactory messageFactory = new MessageFactory(keyPair);
private final NodeKey nodeKey = BouncyCastleNodeKey.generate();
private final MessageFactory messageFactory = new MessageFactory(nodeKey);
private final ConsensusRoundIdentifier roundIdentifier = new ConsensusRoundIdentifier(1, 0);
private final SignedDataValidator signedDataValidator = mock(SignedDataValidator.class);
@ -120,7 +120,7 @@ public class MessageValidatorTest {
final Commit commit =
messageFactory.createCommit(
roundIdentifier, block.getHash(), SECP256K1.sign(block.getHash(), keyPair));
roundIdentifier, block.getHash(), nodeKey.sign(block.getHash()));
assertThat(messageValidator.validateProposal(proposal)).isTrue();
verify(signedDataValidator, times(1)).validateProposal(proposal.getSignedPayload());

@ -22,7 +22,7 @@ import org.hyperledger.besu.consensus.ibft.IbftBlockInterface;
import org.hyperledger.besu.consensus.ibft.TestHelpers;
import org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.ethereum.core.Block;
import java.util.Collections;
@ -36,8 +36,8 @@ import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class ProposalBlockConsistencyValidatorTest {
private final KeyPair proposerKey = KeyPair.generate();
private final MessageFactory proposerMessageFactory = new MessageFactory(proposerKey);
private final MessageFactory proposerMessageFactory =
new MessageFactory(BouncyCastleNodeKey.generate());
private final long chainHeight = 2;
private final ConsensusRoundIdentifier roundIdentifier =
new ConsensusRoundIdentifier(chainHeight, 4);

@ -28,7 +28,8 @@ import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.consensus.ibft.payload.RoundChangeCertificate;
import org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts;
import org.hyperledger.besu.consensus.ibft.validation.RoundChangePayloadValidator.MessageValidatorForHeightFactory;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.Util;
@ -43,9 +44,9 @@ import org.junit.Test;
public class RoundChangeCertificateValidatorTest {
private final KeyPair proposerKey = KeyPair.generate();
private final KeyPair validatorKey = KeyPair.generate();
private final KeyPair otherValidatorKey = KeyPair.generate();
private final NodeKey proposerKey = BouncyCastleNodeKey.generate();
private final NodeKey validatorKey = BouncyCastleNodeKey.generate();
private final NodeKey otherValidatorKey = BouncyCastleNodeKey.generate();
private final MessageFactory proposerMessageFactory = new MessageFactory(proposerKey);
private final MessageFactory validatorMessageFactory = new MessageFactory(validatorKey);
private final List<Address> validators = Lists.newArrayList();

@ -22,7 +22,7 @@ import static org.mockito.Mockito.verify;
import org.hyperledger.besu.consensus.ibft.ConsensusRoundIdentifier;
import org.hyperledger.besu.consensus.ibft.messagewrappers.RoundChange;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.junit.Test;
@ -30,8 +30,7 @@ public class RoundChangeMessageValidatorTest {
private final RoundChangePayloadValidator payloadValidator =
mock(RoundChangePayloadValidator.class);
private final KeyPair keyPair = KeyPair.generate();
private final MessageFactory messageFactory = new MessageFactory(keyPair);
private final MessageFactory messageFactory = new MessageFactory(BouncyCastleNodeKey.generate());
private final ConsensusRoundIdentifier roundIdentifier = new ConsensusRoundIdentifier(1, 1);
private final ProposalBlockConsistencyValidator proposalBlockConsistencyValidator =

@ -29,7 +29,8 @@ import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.consensus.ibft.payload.PreparedCertificate;
import org.hyperledger.besu.consensus.ibft.statemachine.PreparedRoundArtifacts;
import org.hyperledger.besu.consensus.ibft.validation.RoundChangePayloadValidator.MessageValidatorForHeightFactory;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.Hash;
@ -45,9 +46,9 @@ import org.junit.Test;
public class RoundChangeSignedDataValidatorTest {
private final KeyPair proposerKey = KeyPair.generate();
private final KeyPair validatorKey = KeyPair.generate();
private final KeyPair nonValidatorKey = KeyPair.generate();
private final NodeKey proposerKey = BouncyCastleNodeKey.generate();
private final NodeKey validatorKey = BouncyCastleNodeKey.generate();
private final NodeKey nonValidatorKey = BouncyCastleNodeKey.generate();
private final MessageFactory proposerMessageFactory = new MessageFactory(proposerKey);
private final MessageFactory validatorMessageFactory = new MessageFactory(validatorKey);
private final MessageFactory nonValidatorMessageFactory = new MessageFactory(nonValidatorKey);

@ -25,8 +25,8 @@ import org.hyperledger.besu.consensus.ibft.messagewrappers.Commit;
import org.hyperledger.besu.consensus.ibft.messagewrappers.Prepare;
import org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal;
import org.hyperledger.besu.consensus.ibft.payload.MessageFactory;
import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.BouncyCastleNodeKey;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.Hash;
@ -44,9 +44,9 @@ import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class SignedDataValidatorTest {
private final KeyPair proposerKey = KeyPair.generate();
private final KeyPair validatorKey = KeyPair.generate();
private final KeyPair nonValidatorKey = KeyPair.generate();
private final NodeKey proposerKey = BouncyCastleNodeKey.generate();
private final NodeKey validatorKey = BouncyCastleNodeKey.generate();
private final NodeKey nonValidatorKey = BouncyCastleNodeKey.generate();
private final MessageFactory proposerMessageFactory = new MessageFactory(proposerKey);
private final MessageFactory validatorMessageFactory = new MessageFactory(validatorKey);
private final MessageFactory nonValidatorMessageFactory = new MessageFactory(nonValidatorKey);
@ -81,7 +81,7 @@ public class SignedDataValidatorTest {
public void receivingACommitMessageBeforeProposalFails() {
final Commit commitMsg =
proposerMessageFactory.createCommit(
roundIdentifier, Hash.ZERO, SECP256K1.sign(block.getHash(), proposerKey));
roundIdentifier, Hash.ZERO, proposerKey.sign(block.getHash()));
assertThat(validator.validateCommit(commitMsg.getSignedPayload())).isFalse();
}
@ -131,7 +131,7 @@ public class SignedDataValidatorTest {
validatorMessageFactory.createPrepare(invalidRoundIdentifier, block.getHash());
final Commit commitMsg =
validatorMessageFactory.createCommit(
invalidRoundIdentifier, block.getHash(), SECP256K1.sign(block.getHash(), proposerKey));
invalidRoundIdentifier, block.getHash(), proposerKey.sign(block.getHash()));
assertThat(validator.validateProposal(proposalMsg.getSignedPayload())).isTrue();
assertThat(validator.validatePrepare(prepareMsg.getSignedPayload())).isFalse();
@ -156,7 +156,7 @@ public class SignedDataValidatorTest {
final Commit commitMsg =
proposerMessageFactory.createCommit(
roundIdentifier, block.getHash(), SECP256K1.sign(block.getHash(), nonValidatorKey));
roundIdentifier, block.getHash(), nonValidatorKey.sign(block.getHash()));
assertThat(validator.validateProposal(proposalMsg.getSignedPayload())).isTrue();
assertThat(validator.validateCommit(commitMsg.getSignedPayload())).isFalse();
@ -169,11 +169,11 @@ public class SignedDataValidatorTest {
final Commit proposerCommitMsg =
proposerMessageFactory.createCommit(
roundIdentifier, block.getHash(), SECP256K1.sign(block.getHash(), proposerKey));
roundIdentifier, block.getHash(), proposerKey.sign(block.getHash()));
final Commit validatorCommitMsg =
validatorMessageFactory.createCommit(
roundIdentifier, block.getHash(), SECP256K1.sign(block.getHash(), validatorKey));
roundIdentifier, block.getHash(), validatorKey.sign(block.getHash()));
assertThat(validator.validateProposal(proposalMsg.getSignedPayload())).isTrue();
assertThat(validator.validateCommit(proposerCommitMsg.getSignedPayload())).isTrue();

@ -0,0 +1,44 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.crypto;
import org.hyperledger.besu.crypto.SECP256K1.KeyPair;
import org.hyperledger.besu.crypto.SECP256K1.PublicKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.apache.tuweni.bytes.Bytes32;
public class BouncyCastleNodeKey implements NodeKey {
private final KeyPair nodeKeys;
public BouncyCastleNodeKey(final KeyPair nodeKeys) {
this.nodeKeys = nodeKeys;
}
public static BouncyCastleNodeKey generate() {
return new BouncyCastleNodeKey(KeyPair.generate());
}
@Override
public Signature sign(final Bytes32 dataHash) {
return SECP256K1.sign(dataHash, nodeKeys);
}
@Override
public PublicKey getPublicKey() {
return nodeKeys.getPublicKey();
}
}

@ -0,0 +1,27 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.crypto;
import org.hyperledger.besu.crypto.SECP256K1.PublicKey;
import org.hyperledger.besu.crypto.SECP256K1.Signature;
import org.apache.tuweni.bytes.Bytes32;
public interface NodeKey {
Signature sign(Bytes32 dataHash);
PublicKey getPublicKey();
}

@ -17,10 +17,8 @@ package org.hyperledger.besu.ethereum.mainnet.precompiles.privacy;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.hyperledger.besu.enclave.Enclave;
@ -30,7 +28,6 @@ import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockDataGenerator;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Hash;
import org.hyperledger.besu.ethereum.core.Log;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
@ -40,7 +37,6 @@ import org.hyperledger.besu.ethereum.core.WorldUpdater;
import org.hyperledger.besu.ethereum.mainnet.SpuriousDragonGasCalculator;
import org.hyperledger.besu.ethereum.privacy.PrivateTransaction;
import org.hyperledger.besu.ethereum.privacy.PrivateTransactionProcessor;
import org.hyperledger.besu.ethereum.privacy.PrivateTransactionReceipt;
import org.hyperledger.besu.ethereum.privacy.storage.PrivacyGroupHeadBlockMap;
import org.hyperledger.besu.ethereum.privacy.storage.PrivateStateStorage;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
@ -63,20 +59,14 @@ import org.junit.rules.TemporaryFolder;
public class PrivacyPrecompiledContractTest {
@Rule public final TemporaryFolder temp = new TemporaryFolder();
private final String PAYLOAD_TEST_PRIVACY_GROUP_ID =
"8lDVI66RZHIrBsolz6Kn88Rd+WsJ4hUjb4hsh29xW/o=";
private final String DEFAULT_OUTPUT = "0x01";
private final String actual = "Test String";
private final Bytes key = Bytes.wrap(actual.getBytes(UTF_8));
private final WorldStateArchive worldStateArchive = mock(WorldStateArchive.class);
private final PrivateStateStorage privateStateStorage = mock(PrivateStateStorage.class);
private final PrivateStateStorage.Updater storageUpdater =
mock(PrivateStateStorage.Updater.class);
private final Enclave enclave = mock(Enclave.class);
private MessageFrame messageFrame;
private Blockchain blockchain;
private PrivacyPrecompiledContract precompiledContract;
private final String DEFAULT_OUTPUT = "0x01";
final String PAYLOAD_TEST_PRIVACY_GROUP_ID = "8lDVI66RZHIrBsolz6Kn88Rd+WsJ4hUjb4hsh29xW/o=";
private final WorldStateArchive worldStateArchive = mock(WorldStateArchive.class);
final PrivateStateStorage privateStateStorage = mock(PrivateStateStorage.class);
private PrivateTransactionProcessor mockPrivateTxProcessor() {
final PrivateTransactionProcessor mockPrivateTransactionProcessor =
@ -108,6 +98,7 @@ public class PrivacyPrecompiledContractTest {
when(worldStateArchive.getMutable()).thenReturn(mutableWorldState);
when(worldStateArchive.getMutable(any())).thenReturn(Optional.of(mutableWorldState));
final PrivateStateStorage.Updater storageUpdater = mock(PrivateStateStorage.Updater.class);
when(privateStateStorage.getPrivacyGroupHeadBlockMap(any()))
.thenReturn(Optional.of(PrivacyGroupHeadBlockMap.EMPTY));
when(privateStateStorage.getPrivateBlockMetadata(any(), any())).thenReturn(Optional.empty());
@ -133,62 +124,55 @@ public class PrivacyPrecompiledContractTest {
when(blockchain.getBlockByHash(genesis.getHash())).thenReturn(Optional.of(genesis));
when(messageFrame.getBlockchain()).thenReturn(blockchain);
when(messageFrame.getBlockHeader()).thenReturn(block.getHeader());
precompiledContract =
new PrivacyPrecompiledContract(
new SpuriousDragonGasCalculator(), enclave, worldStateArchive, privateStateStorage);
precompiledContract.setPrivateTransactionProcessor(mockPrivateTxProcessor());
}
@Test
public void testPayloadFoundInEnclave() {
mockEnclaveWithPrivateTransaction();
final Enclave enclave = mock(Enclave.class);
final PrivacyPrecompiledContract contract =
new PrivacyPrecompiledContract(
new SpuriousDragonGasCalculator(), enclave, worldStateArchive, privateStateStorage);
contract.setPrivateTransactionProcessor(mockPrivateTxProcessor());
final Bytes actual = precompiledContract.compute(key, messageFrame);
BytesValueRLPOutput bytesValueRLPOutput = new BytesValueRLPOutput();
PrivateTransactionDataFixture.privateTransactionBesu().writeTo(bytesValueRLPOutput);
final ReceiveResponse response =
new ReceiveResponse(
bytesValueRLPOutput.encoded().toBase64String().getBytes(UTF_8),
PAYLOAD_TEST_PRIVACY_GROUP_ID,
null);
when(enclave.receive(any(String.class))).thenReturn(response);
final Bytes actual = contract.compute(key, messageFrame);
assertThat(actual).isEqualTo(Bytes.fromHexString(DEFAULT_OUTPUT));
}
@Test
public void testPayloadNotFoundInEnclave() {
final Enclave enclave = mock(Enclave.class);
final PrivacyPrecompiledContract contract =
new PrivacyPrecompiledContract(
new SpuriousDragonGasCalculator(), enclave, worldStateArchive, privateStateStorage);
when(enclave.receive(any(String.class))).thenThrow(EnclaveClientException.class);
final Bytes expected = precompiledContract.compute(key, messageFrame);
final Bytes expected = contract.compute(key, messageFrame);
assertThat(expected).isEqualTo(Bytes.EMPTY);
}
@Test(expected = RuntimeException.class)
public void testEnclaveDown() {
when(enclave.receive(any(String.class))).thenThrow(new RuntimeException());
precompiledContract.compute(key, messageFrame);
}
final Enclave enclave = mock(Enclave.class);
@Test
public void processTransactionPersistingStateCreatesPrivateReceipt() {
final Hash expectedPmtHash = Hash.wrap(Bytes32.random());
final Hash expectedBlockHash = ((BlockHeader) messageFrame.getBlockHeader()).getHash();
mockEnclaveWithPrivateTransaction();
when(messageFrame.isPersistingPrivateState()).thenReturn(true);
when(messageFrame.getTransactionHash()).thenReturn(expectedPmtHash);
precompiledContract.compute(key, messageFrame);
verify(storageUpdater)
.putTransactionReceipt(
eq(expectedBlockHash), eq(expectedPmtHash), any(PrivateTransactionReceipt.class));
}
final PrivacyPrecompiledContract contract =
new PrivacyPrecompiledContract(
new SpuriousDragonGasCalculator(), enclave, worldStateArchive, privateStateStorage);
private void mockEnclaveWithPrivateTransaction() {
final BytesValueRLPOutput bytesValueRLPOutput = new BytesValueRLPOutput();
PrivateTransactionDataFixture.privateTransactionBesu().writeTo(bytesValueRLPOutput);
when(enclave.receive(any(String.class))).thenThrow(new RuntimeException());
final ReceiveResponse response =
new ReceiveResponse(
bytesValueRLPOutput.encoded().toBase64String().getBytes(UTF_8),
PAYLOAD_TEST_PRIVACY_GROUP_ID,
null);
when(enclave.receive(any(String.class))).thenReturn(response);
contract.compute(key, messageFrame);
}
}

Loading…
Cancel
Save