Search in sources :

Example 6 with NodeKey

use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.

the class RoundChangePayloadValidatorTest method roundChangePayloadSignedByNonValidatorFails.

@Test
public void roundChangePayloadSignedByNonValidatorFails() {
    final RoundChangePayload payload = new RoundChangePayload(new ConsensusRoundIdentifier(chainHeight, 1), Optional.of(new PreparedRoundMetadata(preparedBlockHash, 0)));
    final NodeKey nonValidatorKey = NodeKeyUtils.generate();
    final SignedData<RoundChangePayload> signedPayload = createSignedPayload(payload, nonValidatorKey);
    assertThat(messageValidator.validate(signedPayload)).isFalse();
}
Also used : ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) PreparedRoundMetadata(org.hyperledger.besu.consensus.qbft.payload.PreparedRoundMetadata) RoundChangePayload(org.hyperledger.besu.consensus.qbft.payload.RoundChangePayload) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 7 with NodeKey

use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.

the class QbftRoundTest method exceptionDuringNodeKeySigningDoesNotEscape.

@Test
public void exceptionDuringNodeKeySigningDoesNotEscape() {
    final int QUORUM_SIZE = 1;
    final RoundState roundState = new RoundState(roundIdentifier, QUORUM_SIZE, messageValidator);
    final NodeKey throwingNodeKey = mock(NodeKey.class);
    final MessageFactory throwingMessageFactory = new MessageFactory(throwingNodeKey);
    when(throwingNodeKey.sign(any())).thenThrow(new SecurityModuleException("Hsm is Offline"));
    final QbftRound round = new QbftRound(roundState, blockCreator, protocolContext, blockImporter, subscribers, throwingNodeKey, throwingMessageFactory, transmitter, roundTimer, bftExtraDataCodec);
    round.handleProposalMessage(messageFactory.createProposal(roundIdentifier, proposedBlock, Collections.emptyList(), Collections.emptyList()));
    // Verify that no prepare message was constructed by the QbftRound
    assertThat(roundState.constructPreparedCertificate()).isEmpty();
    verifyNoInteractions(transmitter);
}
Also used : MessageFactory(org.hyperledger.besu.consensus.qbft.payload.MessageFactory) SecurityModuleException(org.hyperledger.besu.plugin.services.securitymodule.SecurityModuleException) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 8 with NodeKey

use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.

the class QbftBlockHeaderValidationRulesetFactoryTest method bftValidateHeaderFailsOnCoinbaseData.

@Test
public void bftValidateHeaderFailsOnCoinbaseData() {
    final NodeKey proposerNodeKey = NodeKeyUtils.generate();
    final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
    final Address nonProposerAddress = Util.publicKeyToAddress(NodeKeyUtils.generate().getPublicKey());
    final List<Address> validators = singletonList(proposerAddress);
    final BlockHeader parentHeader = QbftBlockHeaderUtils.createPresetHeaderBuilder(1, proposerNodeKey, validators, null, Optional.empty()).buildHeader();
    final BlockHeader blockHeader = QbftBlockHeaderUtils.createPresetHeaderBuilder(2, proposerNodeKey, validators, parentHeader, Optional.empty()).coinbase(nonProposerAddress).buildHeader();
    final BlockHeaderValidator validator = getBlockHeaderValidator();
    assertThat(validator.validateHeader(blockHeader, parentHeader, protocolContext(validators), HeaderValidationMode.FULL)).isFalse();
}
Also used : Address(org.hyperledger.besu.datatypes.Address) BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 9 with NodeKey

use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.

the class QbftBlockHeaderValidationRulesetFactoryTest method bftValidateHeaderIgnoresOmmersValue.

@Test
public void bftValidateHeaderIgnoresOmmersValue() {
    final NodeKey proposerNodeKey = NodeKeyUtils.generate();
    final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
    final List<Address> validators = singletonList(proposerAddress);
    final BlockHeader parentHeader = QbftBlockHeaderUtils.createPresetHeaderBuilder(1, proposerNodeKey, validators, null, Optional.empty()).buildHeader();
    final BlockHeader blockHeader = QbftBlockHeaderUtils.createPresetHeaderBuilder(2, proposerNodeKey, validators, parentHeader, builder -> builder.ommersHash(Hash.EMPTY_TRIE_HASH), Optional.empty()).buildHeader();
    final BlockHeaderValidator validator = getBlockHeaderValidator();
    assertThat(validator.validateHeader(blockHeader, parentHeader, protocolContext(validators), HeaderValidationMode.FULL)).isTrue();
}
Also used : NodeKeyUtils(org.hyperledger.besu.crypto.NodeKeyUtils) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) Collections.emptyList(java.util.Collections.emptyList) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collection(java.util.Collection) BftContextBuilder.setupContextWithBftExtraDataEncoder(org.hyperledger.besu.consensus.common.bft.BftContextBuilder.setupContextWithBftExtraDataEncoder) FeeMarket(org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket) Test(org.junit.Test) Address(org.hyperledger.besu.datatypes.Address) Util(org.hyperledger.besu.ethereum.core.Util) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator) Wei(org.hyperledger.besu.datatypes.Wei) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Optional(java.util.Optional) HeaderValidationMode(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode) NodeKey(org.hyperledger.besu.crypto.NodeKey) Hash(org.hyperledger.besu.datatypes.Hash) Address(org.hyperledger.besu.datatypes.Address) BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 10 with NodeKey

use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.

the class QbftBlockHeaderValidationRulesetFactoryTest method bftValidateHeaderFailsOnGasUsage.

@Test
public void bftValidateHeaderFailsOnGasUsage() {
    final NodeKey proposerNodeKey = NodeKeyUtils.generate();
    final Address proposerAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
    final List<Address> validators = singletonList(proposerAddress);
    final BlockHeader parentHeader = QbftBlockHeaderUtils.createPresetHeaderBuilder(1, proposerNodeKey, validators, null, Optional.empty()).buildHeader();
    final BlockHeader blockHeader = QbftBlockHeaderUtils.createPresetHeaderBuilder(2, proposerNodeKey, validators, parentHeader, Optional.empty()).gasLimit(5_000).gasUsed(6_000).buildHeader();
    final BlockHeaderValidator validator = getBlockHeaderValidator();
    assertThat(validator.validateHeader(blockHeader, parentHeader, protocolContext(validators), HeaderValidationMode.FULL)).isFalse();
}
Also used : Address(org.hyperledger.besu.datatypes.Address) BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Aggregations

NodeKey (org.hyperledger.besu.crypto.NodeKey)94 Test (org.junit.Test)78 Address (org.hyperledger.besu.datatypes.Address)43 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)31 BlockHeaderValidator (org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator)24 Bytes (org.apache.tuweni.bytes.Bytes)18 DiscoveryPeer (org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer)17 Endpoint (org.hyperledger.besu.ethereum.p2p.discovery.Endpoint)16 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)14 List (java.util.List)13 ConsensusRoundIdentifier (org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier)13 EnodeURL (org.hyperledger.besu.plugin.data.EnodeURL)13 Optional (java.util.Optional)12 NodeKeyUtils (org.hyperledger.besu.crypto.NodeKeyUtils)11 Collectors (java.util.stream.Collectors)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 Util (org.hyperledger.besu.ethereum.core.Util)9 PeerPermissionsDenylist (org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist)9 Before (org.junit.Before)9 MessageFactory (org.hyperledger.besu.consensus.ibft.payload.MessageFactory)8