use of org.hyperledger.besu.config.BftConfigOptions 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();
}
use of org.hyperledger.besu.config.BftConfigOptions in project besu by hyperledger.
the class IbftForksSchedulesFactoryTest method createsScheduleWithForkThatOverridesGenesisValues.
@Test
public void createsScheduleWithForkThatOverridesGenesisValues() {
final MutableBftConfigOptions configOptions = new MutableBftConfigOptions(JsonBftConfigOptions.DEFAULT);
final ObjectNode fork = JsonUtil.objectNodeFromMap(Map.of(BftFork.FORK_BLOCK_KEY, 1, BftFork.BLOCK_PERIOD_SECONDS_KEY, 10, BftFork.BLOCK_REWARD_KEY, "5"));
final ForksSchedule<BftConfigOptions> forksSchedule = IbftForksSchedulesFactory.create(createGenesisConfig(configOptions, fork));
assertThat(forksSchedule.getFork(0)).usingRecursiveComparison().isEqualTo(new ForkSpec<>(0, configOptions));
final Map<String, Object> forkOptions = new HashMap<>(configOptions.asMap());
forkOptions.put(BftFork.BLOCK_PERIOD_SECONDS_KEY, 10);
forkOptions.put(BftFork.BLOCK_REWARD_KEY, "5");
final BftConfigOptions expectedForkConfig = new MutableBftConfigOptions(new JsonBftConfigOptions(JsonUtil.objectNodeFromMap(forkOptions)));
final ForkSpec<BftConfigOptions> expectedFork = new ForkSpec<>(1, expectedForkConfig);
assertThat(forksSchedule.getFork(1)).usingRecursiveComparison().isEqualTo(expectedFork);
assertThat(forksSchedule.getFork(2)).usingRecursiveComparison().isEqualTo(expectedFork);
}
use of org.hyperledger.besu.config.BftConfigOptions 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));
}
use of org.hyperledger.besu.config.BftConfigOptions in project besu by hyperledger.
the class BaseBftProtocolScheduleTest method zeroEpochLengthThrowsException.
@Test
public void zeroEpochLengthThrowsException() {
final BigInteger arbitraryBlockReward = BigInteger.valueOf(3);
final BftConfigOptions configOptions = createBftConfig(arbitraryBlockReward);
when(configOptions.getEpochLength()).thenReturn(0L);
when(genesisConfig.getBftConfigOptions()).thenReturn(configOptions);
when(genesisConfig.getTransitions()).thenReturn(TransitionsConfigOptions.DEFAULT);
assertThatThrownBy(() -> createProtocolSchedule(List.of(new ForkSpec<>(0, configOptions)))).isInstanceOf(IllegalArgumentException.class).hasMessage("Epoch length in config must be greater than zero");
}
use of org.hyperledger.besu.config.BftConfigOptions in project besu by hyperledger.
the class BaseBftProtocolScheduleTest method missingMiningBeneficiaryInConfigWillPayCoinbaseInHeader.
@Test
public void missingMiningBeneficiaryInConfigWillPayCoinbaseInHeader() {
final BigInteger arbitraryBlockReward = BigInteger.valueOf(3);
final BftConfigOptions configOptions = createBftConfig(arbitraryBlockReward);
when(genesisConfig.getBftConfigOptions()).thenReturn(configOptions);
when(genesisConfig.getTransitions()).thenReturn(TransitionsConfigOptions.DEFAULT);
final ProtocolSchedule schedule = createProtocolSchedule(List.of(new ForkSpec<>(0, configOptions)));
final ProtocolSpec spec = schedule.getByBlockNumber(1);
final Address headerCoinbase = Address.fromHexString("0x123");
final BlockHeader header = mock(BlockHeader.class);
when(header.getCoinbase()).thenReturn(headerCoinbase);
assertThat(spec.getBlockReward()).isEqualTo(Wei.of(arbitraryBlockReward));
assertThat(spec.getMiningBeneficiaryCalculator().calculateBeneficiary(header)).isEqualTo(headerCoinbase);
}
Aggregations