Search in sources :

Example 1 with IbftExtraData

use of org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData in project besu by hyperledger.

the class BftExtraDataValidationRuleTest method createExtraDataWithCommitSeals.

private IbftExtraData createExtraDataWithCommitSeals(final BlockHeader header, final Collection<KeyPair> committerKeyPairs) {
    final IbftExtraData extraDataInHeader = IbftExtraData.decode(header);
    final Hash headerHashForCommitters = IbftBlockHashing.calculateDataHashForCommittedSeal(header, extraDataInHeader);
    final List<SECPSignature> commitSeals = committerKeyPairs.stream().map(keys -> signatureAlgorithm.sign(headerHashForCommitters, keys)).collect(Collectors.toList());
    return new IbftExtraData(extraDataInHeader.getVanityData(), commitSeals, extraDataInHeader.getProposerSeal(), extraDataInHeader.getValidators());
}
Also used : IbftBlockHashing(org.hyperledger.besu.consensus.ibftlegacy.IbftBlockHashing) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) 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) AddressHelpers(org.hyperledger.besu.ethereum.core.AddressHelpers) Test(org.junit.Test) Bytes(org.apache.tuweni.bytes.Bytes) SignatureAlgorithmFactory(org.hyperledger.besu.crypto.SignatureAlgorithmFactory) Address(org.hyperledger.besu.datatypes.Address) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Collectors(java.util.stream.Collectors) Collections.singletonList(java.util.Collections.singletonList) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) List(java.util.List) Lists(com.google.common.collect.Lists) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm) IbftLegacyContextBuilder.setupContextWithValidators(org.hyperledger.besu.consensus.ibftlegacy.IbftLegacyContextBuilder.setupContextWithValidators) KeyPair(org.hyperledger.besu.crypto.KeyPair) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Assertions(org.assertj.core.api.Assertions) Collections(java.util.Collections) Hash(org.hyperledger.besu.datatypes.Hash) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) Hash(org.hyperledger.besu.datatypes.Hash)

Example 2 with IbftExtraData

use of org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData in project besu by hyperledger.

the class BftExtraDataValidationRuleTest method proposerNotInValidatorListFailsValidation.

@Test
public void proposerNotInValidatorListFailsValidation() {
    final BlockHeaderTestFixture builder = new BlockHeaderTestFixture();
    // must NOT be block 0, as that should not contain seals at all
    builder.number(1);
    final KeyPair proposerKeyPair = signatureAlgorithm.generateKeyPair();
    final Address proposerAddress = Address.extract(Hash.hash(proposerKeyPair.getPublicKey().getEncodedBytes()));
    final List<Address> validators = Lists.newArrayList(AddressHelpers.calculateAddressWithRespectTo(proposerAddress, 1), proposerAddress);
    final ProtocolContext context = new ProtocolContext(null, null, setupContextWithValidators(validators));
    final IbftExtraDataValidationRule extraDataValidationRule = new IbftExtraDataValidationRule(true, 0);
    BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators);
    // Insert an extraData block with committer seals.
    final IbftExtraData commitedExtraData = createExtraDataWithCommitSeals(header, singletonList(proposerKeyPair));
    builder.extraData(commitedExtraData.encode());
    header = builder.buildHeader();
    assertThat(extraDataValidationRule.validate(header, null, context)).isFalse();
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Address(org.hyperledger.besu.datatypes.Address) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 3 with IbftExtraData

use of org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData in project besu by hyperledger.

the class BftExtraDataValidationRuleTest method subExecution.

private boolean subExecution(final int validatorCount, final int committerCount, final boolean useTwoThirds) {
    final BlockHeaderTestFixture builder = new BlockHeaderTestFixture();
    // must NOT be block 0, as that should not contain seals at all
    builder.number(1);
    final KeyPair proposerKeyPair = signatureAlgorithm.generateKeyPair();
    final Address proposerAddress = Address.extract(Hash.hash(proposerKeyPair.getPublicKey().getEncodedBytes()));
    final List<Address> validators = Lists.newArrayList();
    final List<KeyPair> committerKeys = Lists.newArrayList();
    validators.add(proposerAddress);
    committerKeys.add(proposerKeyPair);
    for (int i = 0; i < validatorCount - 1; i++) {
        // need -1 to account for proposer
        final KeyPair committerKeyPair = signatureAlgorithm.generateKeyPair();
        committerKeys.add(committerKeyPair);
        validators.add(Address.extract(Hash.hash(committerKeyPair.getPublicKey().getEncodedBytes())));
    }
    Collections.sort(validators);
    BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators);
    final IbftExtraData commitedExtraData = createExtraDataWithCommitSeals(header, committerKeys.subList(0, committerCount));
    builder.extraData(commitedExtraData.encode());
    header = builder.buildHeader();
    final ProtocolContext context = new ProtocolContext(null, null, setupContextWithValidators(validators));
    final IbftExtraDataValidationRule extraDataValidationRule = new IbftExtraDataValidationRule(true, useTwoThirds ? 0 : 2);
    return extraDataValidationRule.validate(header, null, context);
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Address(org.hyperledger.besu.datatypes.Address) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 4 with IbftExtraData

use of org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData in project besu by hyperledger.

the class BftBlockCreatorTest method headerProducedPassesValidationRules.

@Test
public void headerProducedPassesValidationRules() {
    // Construct a parent block.
    final BlockHeaderTestFixture blockHeaderBuilder = new BlockHeaderTestFixture();
    // required to pass validation rule checks.
    blockHeaderBuilder.gasLimit(5000);
    final BlockHeader parentHeader = blockHeaderBuilder.buildHeader();
    final Optional<BlockHeader> optionalHeader = Optional.of(parentHeader);
    // Construct a block chain and world state
    final MutableBlockchain blockchain = mock(MutableBlockchain.class);
    when(blockchain.getChainHeadHash()).thenReturn(parentHeader.getHash());
    when(blockchain.getBlockHeader(any())).thenReturn(optionalHeader);
    final BlockHeader blockHeader = mock(BlockHeader.class);
    when(blockHeader.getBaseFee()).thenReturn(Optional.empty());
    when(blockchain.getChainHeadHeader()).thenReturn(blockHeader);
    final KeyPair nodeKeys = SignatureAlgorithmFactory.getInstance().generateKeyPair();
    // Add the local node as a validator (can't propose a block if node is not a validator).
    final Address localAddr = Address.extract(Hash.hash(nodeKeys.getPublicKey().getEncodedBytes()));
    final List<Address> initialValidatorList = Arrays.asList(Address.fromHexString(String.format("%020d", 1)), Address.fromHexString(String.format("%020d", 2)), Address.fromHexString(String.format("%020d", 3)), Address.fromHexString(String.format("%020d", 4)), localAddr);
    final ProtocolSchedule protocolSchedule = IbftProtocolSchedule.create(GenesisConfigFile.fromConfig("{\"config\": {\"spuriousDragonBlock\":0}}").getConfigOptions(), false, EvmConfiguration.DEFAULT);
    final ProtocolContext protContext = new ProtocolContext(blockchain, createInMemoryWorldStateArchive(), setupContextWithValidators(initialValidatorList));
    final IbftBlockCreator blockCreator = new IbftBlockCreator(Address.fromHexString(String.format("%020d", 0)), () -> Optional.of(10_000_000L), parent -> new IbftExtraData(Bytes.wrap(new byte[32]), Lists.newArrayList(), null, initialValidatorList).encode(), new GasPricePendingTransactionsSorter(TransactionPoolConfiguration.DEFAULT_TX_RETENTION_HOURS, 1, TestClock.fixed(), metricsSystem, blockchain::getChainHeadHeader, TransactionPoolConfiguration.DEFAULT_PRICE_BUMP), protContext, protocolSchedule, nodeKeys, Wei.ZERO, 0.8, parentHeader);
    final Block block = blockCreator.createBlock(Instant.now().getEpochSecond());
    final BlockHeaderValidator rules = IbftBlockHeaderValidationRulesetFactory.ibftProposedBlockValidator(0).build();
    final boolean validationResult = rules.validateHeader(block.getHeader(), parentHeader, protContext, HeaderValidationMode.FULL);
    assertThat(validationResult).isTrue();
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) Address(org.hyperledger.besu.datatypes.Address) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator) IbftProtocolSchedule(org.hyperledger.besu.consensus.ibftlegacy.IbftProtocolSchedule) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) GasPricePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter) Test(org.junit.Test)

Example 5 with IbftExtraData

use of org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData in project besu by hyperledger.

the class BftExtraDataValidationRuleTest method outOfOrderValidatorListFailsValidation.

@Test
public void outOfOrderValidatorListFailsValidation() {
    final BlockHeaderTestFixture builder = new BlockHeaderTestFixture();
    // must NOT be block 0, as that should not contain seals at all
    builder.number(1);
    final KeyPair proposerKeyPair = signatureAlgorithm.generateKeyPair();
    final Address proposerAddress = Address.extract(Hash.hash(proposerKeyPair.getPublicKey().getEncodedBytes()));
    final List<Address> validators = Lists.newArrayList(AddressHelpers.calculateAddressWithRespectTo(proposerAddress, 1), proposerAddress);
    final ProtocolContext context = new ProtocolContext(null, null, setupContextWithValidators(validators));
    final IbftExtraDataValidationRule extraDataValidationRule = new IbftExtraDataValidationRule(true, 0);
    BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators);
    // Insert an extraData block with committer seals.
    final IbftExtraData commitedExtraData = createExtraDataWithCommitSeals(header, singletonList(proposerKeyPair));
    builder.extraData(commitedExtraData.encode());
    header = builder.buildHeader();
    assertThat(extraDataValidationRule.validate(header, null, context)).isFalse();
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Address(org.hyperledger.besu.datatypes.Address) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Aggregations

IbftExtraData (org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData)13 Address (org.hyperledger.besu.datatypes.Address)10 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)10 BlockHeaderTestFixture (org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture)10 KeyPair (org.hyperledger.besu.crypto.KeyPair)9 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)9 Test (org.junit.Test)8 Hash (org.hyperledger.besu.datatypes.Hash)3 SECPSignature (org.hyperledger.besu.crypto.SECPSignature)2 Lists (com.google.common.collect.Lists)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Assertions (org.assertj.core.api.Assertions)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1