Search in sources :

Example 11 with NodeKey

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

the class QbftBlockHeaderValidationRulesetFactoryTest method bftValidateHeaderFailsOnAncestor.

@Test
public void bftValidateHeaderFailsOnAncestor() {
    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, null, Optional.empty()).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 12 with NodeKey

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

the class QbftBlockHeaderValidationRulesetFactoryTest method bftValidateHeaderFailsOnMixHash.

@Test
public void bftValidateHeaderFailsOnMixHash() {
    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.mixHash(Hash.EMPTY_TRIE_HASH), Optional.empty()).buildHeader();
    final BlockHeaderValidator validator = getBlockHeaderValidator();
    assertThat(validator.validateHeader(blockHeader, parentHeader, protocolContext(validators), HeaderValidationMode.FULL)).isFalse();
}
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 13 with NodeKey

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

the class QbftBlockHeaderValidationRulesetFactoryTest method bftValidateHeaderFailsOnDifficulty.

@Test
public void bftValidateHeaderFailsOnDifficulty() {
    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()).difficulty(Difficulty.of(5)).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 14 with NodeKey

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

the class CommitTest method canRoundTripACommitMessage.

@Test
public void canRoundTripACommitMessage() {
    final NodeKey nodeKey = NodeKeyUtils.generate();
    final Address addr = Util.publicKeyToAddress(nodeKey.getPublicKey());
    final CommitPayload commitPayload = new CommitPayload(new ConsensusRoundIdentifier(1, 1), Hash.ZERO, SignatureAlgorithmFactory.getInstance().createSignature(BigInteger.ONE, BigInteger.ONE, (byte) 0));
    final SignedData<CommitPayload> signedCommitPayload = SignedData.create(commitPayload, nodeKey.sign(commitPayload.hashForSignature()));
    final Commit commitMsg = new Commit(signedCommitPayload);
    final Commit decodedPrepare = Commit.decode(commitMsg.encode());
    assertThat(decodedPrepare.getMessageType()).isEqualTo(QbftV1.COMMIT);
    assertThat(decodedPrepare.getAuthor()).isEqualTo(addr);
    assertThat(decodedPrepare.getSignedPayload()).isEqualToComparingFieldByField(signedCommitPayload);
}
Also used : ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) CommitPayload(org.hyperledger.besu.consensus.qbft.payload.CommitPayload) Address(org.hyperledger.besu.datatypes.Address) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 15 with NodeKey

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

the class ProposalTest method canRoundTripProposalMessage.

@Test
public void canRoundTripProposalMessage() {
    final NodeKey nodeKey = NodeKeyUtils.generate();
    final Address addr = Util.publicKeyToAddress(nodeKey.getPublicKey());
    final ProposalPayload payload = new ProposalPayload(new ConsensusRoundIdentifier(1, 1), BLOCK);
    final SignedData<ProposalPayload> signedPayload = SignedData.create(payload, nodeKey.sign(payload.hashForSignature()));
    final PreparePayload preparePayload = new PreparePayload(new ConsensusRoundIdentifier(1, 0), BLOCK.getHash());
    final SignedData<PreparePayload> prepare = SignedData.create(preparePayload, nodeKey.sign(preparePayload.hashForSignature()));
    final RoundChangePayload roundChangePayload = new RoundChangePayload(new ConsensusRoundIdentifier(1, 0), Optional.of(new PreparedRoundMetadata(BLOCK.getHash(), 0)));
    final SignedData<RoundChangePayload> roundChange = SignedData.create(roundChangePayload, nodeKey.sign(roundChangePayload.hashForSignature()));
    final Proposal proposal = new Proposal(signedPayload, List.of(roundChange), List.of(prepare));
    final Proposal decodedProposal = Proposal.decode(proposal.encode(), bftExtraDataCodec);
    assertThat(decodedProposal.getAuthor()).isEqualTo(addr);
    assertThat(decodedProposal.getMessageType()).isEqualTo(QbftV1.PROPOSAL);
    assertThat(decodedProposal.getPrepares()).hasSize(1);
    assertThat(decodedProposal.getPrepares().get(0)).isEqualToComparingFieldByField(prepare);
    assertThat(decodedProposal.getRoundChanges()).hasSize(1);
    assertThat(decodedProposal.getRoundChanges().get(0)).isEqualToComparingFieldByField(roundChange);
    assertThat(decodedProposal.getSignedPayload().getPayload().getProposedBlock()).isEqualTo(BLOCK);
    assertThat(decodedProposal.getSignedPayload().getPayload().getRoundIdentifier()).isEqualTo(payload.getRoundIdentifier());
}
Also used : ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) PreparePayload(org.hyperledger.besu.consensus.qbft.payload.PreparePayload) Address(org.hyperledger.besu.datatypes.Address) PreparedRoundMetadata(org.hyperledger.besu.consensus.qbft.payload.PreparedRoundMetadata) RoundChangePayload(org.hyperledger.besu.consensus.qbft.payload.RoundChangePayload) ProposalPayload(org.hyperledger.besu.consensus.qbft.payload.ProposalPayload) 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