Search in sources :

Example 1 with BlockHeaderValidator

use of org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator in project besu by hyperledger.

the class RlpBlockImporter method validateBlock.

private void validateBlock(final ProtocolSpec protocolSpec, final ProtocolContext context, final BlockHeader previousHeader, final BlockHeader header, final boolean skipPowValidation) {
    final BlockHeaderValidator blockHeaderValidator = protocolSpec.getBlockHeaderValidator();
    final boolean validHeader = blockHeaderValidator.validateHeader(header, previousHeader, context, skipPowValidation ? HeaderValidationMode.LIGHT_DETACHED_ONLY : HeaderValidationMode.DETACHED_ONLY);
    if (!validHeader) {
        throw new IllegalStateException("Invalid header at block number " + header.getNumber() + ".");
    }
}
Also used : BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator)

Example 2 with BlockHeaderValidator

use of org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator 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 3 with BlockHeaderValidator

use of org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator 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 4 with BlockHeaderValidator

use of org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator 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)

Example 5 with BlockHeaderValidator

use of org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator 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)

Aggregations

BlockHeaderValidator (org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator)33 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)29 Address (org.hyperledger.besu.datatypes.Address)28 Test (org.junit.Test)28 NodeKey (org.hyperledger.besu.crypto.NodeKey)24 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)5 ProtocolSpec (org.hyperledger.besu.ethereum.mainnet.ProtocolSpec)4 FeeMarket (org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket)4 Collection (java.util.Collection)3 Collections.emptyList (java.util.Collections.emptyList)3 Collections.singletonList (java.util.Collections.singletonList)3 List (java.util.List)3 Optional (java.util.Optional)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 BftContextBuilder.setupContextWithBftExtraDataEncoder (org.hyperledger.besu.consensus.common.bft.BftContextBuilder.setupContextWithBftExtraDataEncoder)3 KeyPair (org.hyperledger.besu.crypto.KeyPair)3 NodeKeyUtils (org.hyperledger.besu.crypto.NodeKeyUtils)3 Hash (org.hyperledger.besu.datatypes.Hash)3 Wei (org.hyperledger.besu.datatypes.Wei)3 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)3