Search in sources :

Example 1 with ForksSchedule

use of org.hyperledger.besu.consensus.common.ForksSchedule in project besu by hyperledger.

the class BaseBftProtocolSchedule method createProtocolSchedule.

public ProtocolSchedule createProtocolSchedule(final GenesisConfigOptions config, final ForksSchedule<? extends BftConfigOptions> forksSchedule, final PrivacyParameters privacyParameters, final boolean isRevertReasonEnabled, final BftExtraDataCodec bftExtraDataCodec, final EvmConfiguration evmConfiguration) {
    final Map<Long, Function<ProtocolSpecBuilder, ProtocolSpecBuilder>> specMap = new HashMap<>();
    forksSchedule.getForks().forEach(forkSpec -> specMap.put(forkSpec.getBlock(), builder -> applyBftChanges(builder, forkSpec.getValue(), config.isQuorum(), bftExtraDataCodec)));
    final ProtocolSpecAdapters specAdapters = new ProtocolSpecAdapters(specMap);
    return new ProtocolScheduleBuilder(config, DEFAULT_CHAIN_ID, specAdapters, privacyParameters, isRevertReasonEnabled, config.isQuorum(), evmConfiguration).createProtocolSchedule();
}
Also used : MainnetProtocolSpecs(org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSpecs) ProtocolSpecBuilder(org.hyperledger.besu.ethereum.mainnet.ProtocolSpecBuilder) ProtocolSpecAdapters(org.hyperledger.besu.ethereum.mainnet.ProtocolSpecAdapters) ProtocolScheduleBuilder(org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder) FeeMarket(org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket) HashMap(java.util.HashMap) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) EvmConfiguration(org.hyperledger.besu.evm.internal.EvmConfiguration) Function(java.util.function.Function) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) BftConfigOptions(org.hyperledger.besu.config.BftConfigOptions) MainnetBlockBodyValidator(org.hyperledger.besu.ethereum.mainnet.MainnetBlockBodyValidator) MainnetBlockImporter(org.hyperledger.besu.ethereum.mainnet.MainnetBlockImporter) ForksSchedule(org.hyperledger.besu.consensus.common.ForksSchedule) BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator) Map(java.util.Map) Wei(org.hyperledger.besu.datatypes.Wei) BigInteger(java.math.BigInteger) PrivacyParameters(org.hyperledger.besu.ethereum.core.PrivacyParameters) Function(java.util.function.Function) HashMap(java.util.HashMap) ProtocolSpecAdapters(org.hyperledger.besu.ethereum.mainnet.ProtocolSpecAdapters) ProtocolScheduleBuilder(org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder)

Example 2 with ForksSchedule

use of org.hyperledger.besu.consensus.common.ForksSchedule in project besu by hyperledger.

the class ForkingValidatorProviderTest method getVoteProviderAfterBlock_correctVoteProviderIsResolved.

@Test
public void getVoteProviderAfterBlock_correctVoteProviderIsResolved() {
    final ForksSchedule<QbftConfigOptions> forksSchedule = new ForksSchedule<>(List.of(createBlockForkSpec(0), createBlockForkSpec(1), createContractForkSpec(2, CONTRACT_ADDRESS_1)));
    final ForkingValidatorProvider validatorProvider = new ForkingValidatorProvider(blockChain, forksSchedule, blockValidatorProvider, contractValidatorProvider);
    final VoteProvider voteProviderForBlockValidator = Mockito.mock(VoteProvider.class);
    when(blockValidatorProvider.getVoteProviderAtHead()).thenReturn(Optional.of(voteProviderForBlockValidator));
    when(contractValidatorProvider.getVoteProviderAtHead()).thenReturn(Optional.empty());
    SoftAssertions.assertSoftly((softly) -> {
        softly.assertThat(validatorProvider.getVoteProviderAfterBlock(genesisHeader)).contains(voteProviderForBlockValidator);
        softly.assertThat(validatorProvider.getVoteProviderAfterBlock(header1)).isEmpty();
        softly.assertThat(validatorProvider.getVoteProviderAfterBlock(header2)).isEmpty();
    });
}
Also used : VoteProvider(org.hyperledger.besu.consensus.common.validator.VoteProvider) QbftConfigOptions(org.hyperledger.besu.config.QbftConfigOptions) ForksSchedule(org.hyperledger.besu.consensus.common.ForksSchedule) Test(org.junit.Test)

Example 3 with ForksSchedule

use of org.hyperledger.besu.consensus.common.ForksSchedule in project besu by hyperledger.

the class QbftForksSchedulesFactoryTest method switchingToBlockHeaderRemovesValidatorContractAddress.

@Test
public void switchingToBlockHeaderRemovesValidatorContractAddress() {
    final MutableQbftConfigOptions configOptions = new MutableQbftConfigOptions(JsonQbftConfigOptions.DEFAULT);
    configOptions.setValidatorContractAddress(Optional.of("10"));
    List<Address> validators = List.of(AddressHelpers.ofValue(1));
    final List<TextNode> jsonValidators = validators.stream().map(v -> TextNode.valueOf(v.toHexString())).collect(Collectors.toList());
    final ObjectNode fork = JsonUtil.objectNodeFromMap(Map.of(BftFork.FORK_BLOCK_KEY, 1, QbftFork.VALIDATOR_SELECTION_MODE_KEY, VALIDATOR_SELECTION_MODE.BLOCKHEADER, BftFork.VALIDATORS_KEY, JsonUtil.getObjectMapper().createArrayNode().addAll(jsonValidators)));
    final ForksSchedule<QbftConfigOptions> forksSchedule = QbftForksSchedulesFactory.create(createGenesisConfig(configOptions, fork));
    assertThat(forksSchedule.getFork(1).getValue().getValidatorContractAddress()).isEmpty();
}
Also used : Arrays(java.util.Arrays) HashMap(java.util.HashMap) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) BaseForksSchedulesFactoryTest(org.hyperledger.besu.consensus.common.bft.BaseForksSchedulesFactoryTest) QbftFork(org.hyperledger.besu.config.QbftFork) Address(org.hyperledger.besu.datatypes.Address) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ForkSpec(org.hyperledger.besu.consensus.common.ForkSpec) ForksSchedule(org.hyperledger.besu.consensus.common.ForksSchedule) Map(java.util.Map) AssertionsForClassTypes.assertThatThrownBy(org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) BftFork(org.hyperledger.besu.config.BftFork) QbftForksSchedulesFactory(org.hyperledger.besu.consensus.qbft.QbftForksSchedulesFactory) TransitionsConfigOptions(org.hyperledger.besu.config.TransitionsConfigOptions) StubGenesisConfigOptions(org.hyperledger.besu.config.StubGenesisConfigOptions) AddressHelpers(org.hyperledger.besu.ethereum.core.AddressHelpers) Test(org.junit.Test) JsonUtil(org.hyperledger.besu.config.JsonUtil) MutableQbftConfigOptions(org.hyperledger.besu.consensus.qbft.MutableQbftConfigOptions) QbftConfigOptions(org.hyperledger.besu.config.QbftConfigOptions) Collectors(java.util.stream.Collectors) TextNode(com.fasterxml.jackson.databind.node.TextNode) JsonQbftConfigOptions(org.hyperledger.besu.config.JsonQbftConfigOptions) Consumer(java.util.function.Consumer) List(java.util.List) VALIDATOR_SELECTION_MODE(org.hyperledger.besu.config.QbftFork.VALIDATOR_SELECTION_MODE) Optional(java.util.Optional) Address(org.hyperledger.besu.datatypes.Address) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) MutableQbftConfigOptions(org.hyperledger.besu.consensus.qbft.MutableQbftConfigOptions) QbftConfigOptions(org.hyperledger.besu.config.QbftConfigOptions) JsonQbftConfigOptions(org.hyperledger.besu.config.JsonQbftConfigOptions) TextNode(com.fasterxml.jackson.databind.node.TextNode) MutableQbftConfigOptions(org.hyperledger.besu.consensus.qbft.MutableQbftConfigOptions) BaseForksSchedulesFactoryTest(org.hyperledger.besu.consensus.common.bft.BaseForksSchedulesFactoryTest) Test(org.junit.Test)

Example 4 with ForksSchedule

use of org.hyperledger.besu.consensus.common.ForksSchedule in project besu by hyperledger.

the class BftBlockCreatorTest method createdBlockPassesValidationRulesAndHasAppropriateHashAndMixHash.

@Test
public void createdBlockPassesValidationRulesAndHasAppropriateHashAndMixHash() {
    // 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 List<Address> initialValidatorList = Lists.newArrayList();
    for (int i = 0; i < 4; i++) {
        initialValidatorList.add(AddressHelpers.ofValue(i));
    }
    final IbftExtraDataCodec bftExtraDataEncoder = new IbftExtraDataCodec();
    final BaseBftProtocolSchedule bftProtocolSchedule = new BaseBftProtocolSchedule() {

        @Override
        public BlockHeaderValidator.Builder createBlockHeaderRuleset(final BftConfigOptions config, final FeeMarket feeMarket) {
            return IbftBlockHeaderValidationRulesetFactory.blockHeaderValidator(5, Optional.empty());
        }
    };
    final GenesisConfigOptions configOptions = GenesisConfigFile.fromConfig("{\"config\": {\"spuriousDragonBlock\":0}}").getConfigOptions();
    final ForksSchedule<BftConfigOptions> forksSchedule = new ForksSchedule<>(List.of(new ForkSpec<>(0, configOptions.getBftConfigOptions())));
    final ProtocolSchedule protocolSchedule = bftProtocolSchedule.createProtocolSchedule(configOptions, forksSchedule, PrivacyParameters.DEFAULT, false, bftExtraDataEncoder, EvmConfiguration.DEFAULT);
    final ProtocolContext protContext = new ProtocolContext(blockchain, createInMemoryWorldStateArchive(), setupContextWithBftExtraDataEncoder(initialValidatorList, bftExtraDataEncoder));
    final GasPricePendingTransactionsSorter pendingTransactions = new GasPricePendingTransactionsSorter(TransactionPoolConfiguration.DEFAULT_TX_RETENTION_HOURS, 1, TestClock.fixed(), metricsSystem, blockchain::getChainHeadHeader, TransactionPoolConfiguration.DEFAULT_PRICE_BUMP);
    final BftBlockCreator blockCreator = new BftBlockCreator(forksSchedule, initialValidatorList.get(0), () -> Optional.of(10_000_000L), parent -> bftExtraDataEncoder.encode(new BftExtraData(Bytes.wrap(new byte[32]), Collections.emptyList(), Optional.empty(), 0, initialValidatorList)), pendingTransactions, protContext, protocolSchedule, Wei.ZERO, 0.8, parentHeader, bftExtraDataEncoder);
    final int secondsBetweenBlocks = 1;
    final Block block = blockCreator.createBlock(parentHeader.getTimestamp() + 1);
    final BlockHeaderValidator rules = IbftBlockHeaderValidationRulesetFactory.blockHeaderValidator(secondsBetweenBlocks, Optional.empty()).build();
    // NOTE: The header will not contain commit seals, so can only do light validation on header.
    final boolean validationResult = rules.validateHeader(block.getHeader(), parentHeader, protContext, HeaderValidationMode.LIGHT);
    assertThat(validationResult).isTrue();
    final BlockHeader header = block.getHeader();
    final BftExtraData extraData = bftExtraDataEncoder.decode(header);
    assertThat(block.getHash()).isEqualTo(new BftBlockHashing(bftExtraDataEncoder).calculateDataHashForCommittedSeal(header, extraData));
}
Also used : Address(org.hyperledger.besu.datatypes.Address) ForkSpec(org.hyperledger.besu.consensus.common.ForkSpec) BftConfigOptions(org.hyperledger.besu.config.BftConfigOptions) BftBlockCreator(org.hyperledger.besu.consensus.common.bft.blockcreation.BftBlockCreator) IbftExtraDataCodec(org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec) FeeMarket(org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) BaseBftProtocolSchedule(org.hyperledger.besu.consensus.common.bft.BaseBftProtocolSchedule) BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator) ForksSchedule(org.hyperledger.besu.consensus.common.ForksSchedule) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) BaseBftProtocolSchedule(org.hyperledger.besu.consensus.common.bft.BaseBftProtocolSchedule) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) BftBlockHashing(org.hyperledger.besu.consensus.common.bft.BftBlockHashing) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) GasPricePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter) Test(org.junit.Test)

Aggregations

ForksSchedule (org.hyperledger.besu.consensus.common.ForksSchedule)4 GenesisConfigOptions (org.hyperledger.besu.config.GenesisConfigOptions)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BftConfigOptions (org.hyperledger.besu.config.BftConfigOptions)2 QbftConfigOptions (org.hyperledger.besu.config.QbftConfigOptions)2 ForkSpec (org.hyperledger.besu.consensus.common.ForkSpec)2 Address (org.hyperledger.besu.datatypes.Address)2 BlockHeaderValidator (org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator)2 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)2 FeeMarket (org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 BigInteger (java.math.BigInteger)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1