Search in sources :

Example 1 with ForkSpec

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

the class QbftProtocolScheduleTest method blockModeTransitionsCreatesBlockModeHeaderValidators.

@Test
public void blockModeTransitionsCreatesBlockModeHeaderValidators() {
    final MutableQbftConfigOptions arbitraryTransition = new MutableQbftConfigOptions(JsonQbftConfigOptions.DEFAULT);
    arbitraryTransition.setBlockRewardWei(BigInteger.ONE);
    final BlockHeader parentHeader = QbftBlockHeaderUtils.createPresetHeaderBuilder(1, proposerNodeKey, validators, null, Optional.empty()).buildHeader();
    final BlockHeader blockHeader = QbftBlockHeaderUtils.createPresetHeaderBuilder(2, proposerNodeKey, validators, parentHeader, Optional.empty()).buildHeader();
    final ProtocolSchedule schedule = createProtocolSchedule(JsonGenesisConfigOptions.fromJsonObject(JsonUtil.createEmptyObjectNode()), List.of(new ForkSpec<>(0, JsonQbftConfigOptions.DEFAULT), new ForkSpec<>(1, arbitraryTransition), new ForkSpec<>(2, JsonQbftConfigOptions.DEFAULT)));
    assertThat(schedule.streamMilestoneBlocks().count()).isEqualTo(3);
    assertThat(validateHeader(schedule, validators, parentHeader, blockHeader, 0)).isTrue();
    assertThat(validateHeader(schedule, validators, parentHeader, blockHeader, 1)).isTrue();
    assertThat(validateHeader(schedule, validators, parentHeader, blockHeader, 2)).isTrue();
}
Also used : ForkSpec(org.hyperledger.besu.consensus.common.ForkSpec) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) Test(org.junit.Test)

Example 2 with ForkSpec

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

the class QbftBlockCreatorFactoryTest method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    final MiningParameters miningParams = mock(MiningParameters.class);
    when(miningParams.getExtraData()).thenReturn(Bytes.wrap("Qbft tests".getBytes(UTF_8)));
    final MutableQbftConfigOptions qbftConfigOptions = new MutableQbftConfigOptions(JsonQbftConfigOptions.DEFAULT);
    qbftConfigOptions.setValidatorContractAddress(Optional.of("1"));
    final ForkSpec<QbftConfigOptions> spec = new ForkSpec<>(0, qbftConfigOptions);
    final ForksSchedule<QbftConfigOptions> forksSchedule = mock(ForksSchedule.class);
    when(forksSchedule.getFork(anyLong())).thenReturn(spec);
    qbftBlockCreatorFactory = new QbftBlockCreatorFactory(mock(AbstractPendingTransactionsSorter.class), mock(ProtocolContext.class), mock(ProtocolSchedule.class), forksSchedule, miningParams, mock(Address.class), extraDataCodec);
}
Also used : MiningParameters(org.hyperledger.besu.ethereum.core.MiningParameters) ForkSpec(org.hyperledger.besu.consensus.common.ForkSpec) MutableQbftConfigOptions(org.hyperledger.besu.consensus.qbft.MutableQbftConfigOptions) QbftConfigOptions(org.hyperledger.besu.config.QbftConfigOptions) JsonQbftConfigOptions(org.hyperledger.besu.config.JsonQbftConfigOptions) MutableQbftConfigOptions(org.hyperledger.besu.consensus.qbft.MutableQbftConfigOptions) Before(org.junit.Before)

Example 3 with ForkSpec

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

the class ConsensusScheduleBesuControllerBuilderTest method createsMigratingContext.

@Test
public void createsMigratingContext() {
    final ConsensusContext context1 = Mockito.mock(ConsensusContext.class);
    final ConsensusContext context2 = Mockito.mock(ConsensusContext.class);
    final Map<Long, BesuControllerBuilder> besuControllerBuilderSchedule = new TreeMap<>();
    besuControllerBuilderSchedule.put(0L, besuControllerBuilder1);
    besuControllerBuilderSchedule.put(10L, besuControllerBuilder2);
    when(besuControllerBuilder1.createConsensusContext(any(), any(), any())).thenReturn(context1);
    when(besuControllerBuilder2.createConsensusContext(any(), any(), any())).thenReturn(context2);
    final ConsensusScheduleBesuControllerBuilder controllerBuilder = new ConsensusScheduleBesuControllerBuilder(besuControllerBuilderSchedule);
    final ConsensusContext consensusContext = controllerBuilder.createConsensusContext(Mockito.mock(Blockchain.class), Mockito.mock(WorldStateArchive.class), Mockito.mock(ProtocolSchedule.class));
    assertThat(consensusContext).isInstanceOf(MigratingContext.class);
    final MigratingContext migratingContext = (MigratingContext) consensusContext;
    final ForksSchedule<ConsensusContext> contextSchedule = migratingContext.getConsensusContextSchedule();
    final NavigableSet<ForkSpec<ConsensusContext>> expectedConsensusContextSpecs = new TreeSet<>(ForkSpec.COMPARATOR);
    expectedConsensusContextSpecs.add(new ForkSpec<>(0L, context1));
    expectedConsensusContextSpecs.add(new ForkSpec<>(10L, context2));
    assertThat(contextSchedule.getForks()).isEqualTo(expectedConsensusContextSpecs);
    assertThat(contextSchedule.getFork(0).getValue()).isSameAs(context1);
    assertThat(contextSchedule.getFork(1).getValue()).isSameAs(context1);
    assertThat(contextSchedule.getFork(9).getValue()).isSameAs(context1);
    assertThat(contextSchedule.getFork(10).getValue()).isSameAs(context2);
    assertThat(contextSchedule.getFork(11).getValue()).isSameAs(context2);
}
Also used : ForkSpec(org.hyperledger.besu.consensus.common.ForkSpec) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) TreeMap(java.util.TreeMap) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) MigratingContext(org.hyperledger.besu.consensus.common.MigratingContext) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) TreeSet(java.util.TreeSet) ConsensusContext(org.hyperledger.besu.ethereum.ConsensusContext) Test(org.junit.Test)

Example 4 with ForkSpec

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

the class ConsensusScheduleBesuControllerBuilderTest method mustCreateCombinedProtocolScheduleUsingProtocolSchedulesOrderedByBlock.

@Test
public void mustCreateCombinedProtocolScheduleUsingProtocolSchedulesOrderedByBlock() {
    // Use an ordered map with keys in the incorrect order so that we can show that set is created
    // with the correct order
    final Map<Long, BesuControllerBuilder> besuControllerBuilderSchedule = new TreeMap<>();
    besuControllerBuilderSchedule.put(30L, besuControllerBuilder3);
    besuControllerBuilderSchedule.put(10L, besuControllerBuilder2);
    besuControllerBuilderSchedule.put(0L, besuControllerBuilder1);
    when(besuControllerBuilder1.createProtocolSchedule()).thenReturn(protocolSchedule1);
    when(besuControllerBuilder2.createProtocolSchedule()).thenReturn(protocolSchedule2);
    when(besuControllerBuilder3.createProtocolSchedule()).thenReturn(protocolSchedule3);
    final StubGenesisConfigOptions genesisConfigOptions = new StubGenesisConfigOptions();
    genesisConfigOptions.chainId(BigInteger.TEN);
    when(genesisConfigFile.getConfigOptions()).thenReturn(genesisConfigOptions);
    final ConsensusScheduleBesuControllerBuilder consensusScheduleBesuControllerBuilder = new ConsensusScheduleBesuControllerBuilder(besuControllerBuilderSchedule, combinedProtocolScheduleFactory);
    consensusScheduleBesuControllerBuilder.genesisConfigFile(genesisConfigFile);
    consensusScheduleBesuControllerBuilder.createProtocolSchedule();
    final NavigableSet<ForkSpec<ProtocolSchedule>> expectedProtocolSchedulesSpecs = new TreeSet<>(ForkSpec.COMPARATOR);
    expectedProtocolSchedulesSpecs.add(new ForkSpec<>(0L, protocolSchedule1));
    expectedProtocolSchedulesSpecs.add(new ForkSpec<>(10L, protocolSchedule2));
    expectedProtocolSchedulesSpecs.add(new ForkSpec<>(30L, protocolSchedule3));
    Mockito.verify(combinedProtocolScheduleFactory).apply(expectedProtocolSchedulesSpecs, Optional.of(BigInteger.TEN));
}
Also used : ForkSpec(org.hyperledger.besu.consensus.common.ForkSpec) TreeSet(java.util.TreeSet) TreeMap(java.util.TreeMap) StubGenesisConfigOptions(org.hyperledger.besu.config.StubGenesisConfigOptions) Test(org.junit.Test)

Example 5 with ForkSpec

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

the class QbftForksSchedulesFactoryTest method createsScheduleWithForkThatOverridesGenesisValues.

@Test
public void createsScheduleWithForkThatOverridesGenesisValues() {
    final MutableQbftConfigOptions configOptions = new MutableQbftConfigOptions(JsonQbftConfigOptions.DEFAULT);
    final ObjectNode fork = JsonUtil.objectNodeFromMap(Map.of(BftFork.FORK_BLOCK_KEY, 1, BftFork.VALIDATORS_KEY, List.of("1", "2", "3"), BftFork.BLOCK_PERIOD_SECONDS_KEY, 10, BftFork.BLOCK_REWARD_KEY, "5", QbftFork.VALIDATOR_SELECTION_MODE_KEY, VALIDATOR_SELECTION_MODE.CONTRACT, QbftFork.VALIDATOR_CONTRACT_ADDRESS_KEY, "10"));
    final ForksSchedule<QbftConfigOptions> forksSchedule = QbftForksSchedulesFactory.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");
    forkOptions.put(QbftFork.VALIDATOR_SELECTION_MODE_KEY, "5");
    forkOptions.put(QbftFork.VALIDATOR_CONTRACT_ADDRESS_KEY, "10");
    final QbftConfigOptions expectedForkConfig = new MutableQbftConfigOptions(new JsonQbftConfigOptions(JsonUtil.objectNodeFromMap(forkOptions)));
    final ForkSpec<QbftConfigOptions> expectedFork = new ForkSpec<>(1, expectedForkConfig);
    assertThat(forksSchedule.getFork(1)).usingRecursiveComparison().isEqualTo(expectedFork);
    assertThat(forksSchedule.getFork(2)).usingRecursiveComparison().isEqualTo(expectedFork);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ForkSpec(org.hyperledger.besu.consensus.common.ForkSpec) HashMap(java.util.HashMap) MutableQbftConfigOptions(org.hyperledger.besu.consensus.qbft.MutableQbftConfigOptions) QbftConfigOptions(org.hyperledger.besu.config.QbftConfigOptions) JsonQbftConfigOptions(org.hyperledger.besu.config.JsonQbftConfigOptions) JsonQbftConfigOptions(org.hyperledger.besu.config.JsonQbftConfigOptions) MutableQbftConfigOptions(org.hyperledger.besu.consensus.qbft.MutableQbftConfigOptions) BaseForksSchedulesFactoryTest(org.hyperledger.besu.consensus.common.bft.BaseForksSchedulesFactoryTest) Test(org.junit.Test)

Aggregations

ForkSpec (org.hyperledger.besu.consensus.common.ForkSpec)16 Test (org.junit.Test)14 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)9 BftConfigOptions (org.hyperledger.besu.config.BftConfigOptions)7 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)7 JsonBftConfigOptions (org.hyperledger.besu.config.JsonBftConfigOptions)6 Address (org.hyperledger.besu.datatypes.Address)5 BigInteger (java.math.BigInteger)4 GenesisConfigOptions (org.hyperledger.besu.config.GenesisConfigOptions)3 MutableQbftConfigOptions (org.hyperledger.besu.consensus.qbft.MutableQbftConfigOptions)3 ProtocolSpec (org.hyperledger.besu.ethereum.mainnet.ProtocolSpec)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 HashMap (java.util.HashMap)2 TreeMap (java.util.TreeMap)2 TreeSet (java.util.TreeSet)2 JsonQbftConfigOptions (org.hyperledger.besu.config.JsonQbftConfigOptions)2 QbftConfigOptions (org.hyperledger.besu.config.QbftConfigOptions)2 ForksSchedule (org.hyperledger.besu.consensus.common.ForksSchedule)2 BaseForksSchedulesFactoryTest (org.hyperledger.besu.consensus.common.bft.BaseForksSchedulesFactoryTest)2 MutableBftConfigOptions (org.hyperledger.besu.consensus.common.bft.MutableBftConfigOptions)2