Skip to content

Commit

Permalink
Change the ForkSchedule to take a single argument (hyperledger#3133)
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Frame <jasonwframe@gmail.com>
  • Loading branch information
jframe authored Dec 7, 2021
1 parent db749c1 commit af4221a
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ protected MiningCoordinator createMiningCoordinator(
ethProtocolManager)))
.collect(Collectors.toList());
final ForksSchedule<MiningCoordinator> miningCoordinatorSchedule =
new ForksSchedule<>(
miningCoordinatorForkSpecs.get(0),
miningCoordinatorForkSpecs.subList(1, miningCoordinatorForkSpecs.size()));
new ForksSchedule<>(miningCoordinatorForkSpecs);

return new MigratingMiningCoordinator(
miningCoordinatorSchedule, protocolContext.getBlockchain());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public class ForksSchedule<C> {
new TreeSet<>(
Comparator.comparing((Function<ForkSpec<C>, Long>) ForkSpec::getBlock).reversed());

public ForksSchedule(final ForkSpec<C> genesisFork, final Collection<ForkSpec<C>> forks) {
this.forks.add(genesisFork);
public ForksSchedule(final Collection<ForkSpec<C>> forks) {
this.forks.addAll(forks);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public static <T extends BftConfigOptions, U extends BftFork> ForksSchedule<T> c
specs.add(new ForkSpec<>(f.getForkBlock(), spec));
});

return new ForksSchedule<>(initialForkSpec, specs.tailSet(initialForkSpec, false));
return new ForksSchedule<>(specs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public class ForksScheduleTest {
public void retrievesGenesisFork() {
final ForkSpec<BftConfigOptions> genesisForkSpec =
new ForkSpec<>(0, JsonBftConfigOptions.DEFAULT);
final ForkSpec<BftConfigOptions> forkSpec1 = createForkSpec(10, 10);

final ForksSchedule<BftConfigOptions> schedule =
new ForksSchedule<>(genesisForkSpec, List.of());
new ForksSchedule<>(List.of(forkSpec1, genesisForkSpec));
assertThat(schedule.getFork(0)).isEqualTo(genesisForkSpec);
assertThat(schedule.getFork(1)).isEqualTo(genesisForkSpec);
}
Expand All @@ -45,7 +46,7 @@ public void retrievesLatestFork() {
final ForkSpec<BftConfigOptions> forkSpec2 = createForkSpec(2, 20);

final ForksSchedule<BftConfigOptions> schedule =
new ForksSchedule<>(genesisForkSpec, List.of(forkSpec1, forkSpec2));
new ForksSchedule<>(List.of(genesisForkSpec, forkSpec1, forkSpec2));

assertThat(schedule.getFork(0)).isEqualTo(genesisForkSpec);
assertThat(schedule.getFork(1)).isEqualTo(forkSpec1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void setup() {
new ForkSpec<>(GENESIS_BLOCK_NUMBER, coordinator1);
final ForkSpec<MiningCoordinator> migrationFork =
new ForkSpec<>(MIGRATION_BLOCK_NUMBER, coordinator2);
miningCoordinatorSchedule = new ForksSchedule<>(genesisFork, List.of(migrationFork));
miningCoordinatorSchedule = new ForksSchedule<>(List.of(genesisFork, migrationFork));
this.block = new Block(blockHeader, blockBody);
blockEvent = BlockAddedEvent.createForHeadAdvancement(this.block, emptyList(), emptyList());
}
Expand Down Expand Up @@ -101,7 +101,7 @@ public void onBlockAddedShouldMigrateToNextMiningCoordinatorAndDelegate() {
@Test
public void onBlockAddedShouldNotDelegateWhenDelegateIsNoop() {
ForksSchedule<MiningCoordinator> noopCoordinatorSchedule =
new ForksSchedule<>(new ForkSpec<>(GENESIS_BLOCK_NUMBER, noopCoordinator), emptyList());
new ForksSchedule<>(List.of(new ForkSpec<>(GENESIS_BLOCK_NUMBER, noopCoordinator)));
when(blockHeader.getNumber()).thenReturn(GENESIS_BLOCK_NUMBER);

new MigratingMiningCoordinator(noopCoordinatorSchedule, blockchain).onBlockAdded(blockEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.hyperledger.besu.evm.internal.EvmConfiguration;

import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
Expand All @@ -60,7 +59,7 @@ public void ensureBlockRewardAndMiningBeneficiaryInProtocolSpecMatchConfig() {
when(genesisConfig.getBftConfigOptions()).thenReturn(configOptions);

final ProtocolSchedule schedule =
createProtocolSchedule(new ForkSpec<>(0, configOptions), Collections.emptyList());
createProtocolSchedule(List.of(new ForkSpec<>(0, configOptions)));
final ProtocolSpec spec = schedule.getByBlockNumber(1);

assertThat(spec.getBlockReward()).isEqualTo(Wei.of(arbitraryBlockReward));
Expand All @@ -77,8 +76,7 @@ public void illegalMiningBeneficiaryStringThrowsException() {
when(configOptions.getEpochLength()).thenReturn(3000L);
when(configOptions.getBlockRewardWei()).thenReturn(BigInteger.ZERO);
when(genesisConfig.getTransitions()).thenReturn(TransitionsConfigOptions.DEFAULT);
assertThatThrownBy(
() -> createProtocolSchedule(new ForkSpec<>(0, configOptions), Collections.emptyList()))
assertThatThrownBy(() -> createProtocolSchedule(List.of(new ForkSpec<>(0, configOptions))))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Mining beneficiary in config is not a valid ethereum address");
}
Expand All @@ -94,7 +92,7 @@ public void missingMiningBeneficiaryInConfigWillPayCoinbaseInHeader() {
when(genesisConfig.getTransitions()).thenReturn(TransitionsConfigOptions.DEFAULT);

final ProtocolSchedule schedule =
createProtocolSchedule(new ForkSpec<>(0, configOptions), Collections.emptyList());
createProtocolSchedule(List.of(new ForkSpec<>(0, configOptions)));
final ProtocolSpec spec = schedule.getByBlockNumber(1);

final Address headerCoinbase = Address.fromHexString("0x123");
Expand All @@ -116,8 +114,7 @@ public void negativeBlockRewardThrowsException() {
when(genesisConfig.getBftConfigOptions()).thenReturn(configOptions);
when(genesisConfig.getTransitions()).thenReturn(TransitionsConfigOptions.DEFAULT);

assertThatThrownBy(
() -> createProtocolSchedule(new ForkSpec<>(0, configOptions), Collections.emptyList()))
assertThatThrownBy(() -> createProtocolSchedule(List.of(new ForkSpec<>(0, configOptions))))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Bft Block reward in config cannot be negative");
}
Expand All @@ -132,8 +129,7 @@ public void zeroEpochLengthThrowsException() {
when(genesisConfig.getBftConfigOptions()).thenReturn(configOptions);
when(genesisConfig.getTransitions()).thenReturn(TransitionsConfigOptions.DEFAULT);

assertThatThrownBy(
() -> createProtocolSchedule(new ForkSpec<>(0, configOptions), Collections.emptyList()))
assertThatThrownBy(() -> createProtocolSchedule(List.of(new ForkSpec<>(0, configOptions))))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Epoch length in config must be greater than zero");
}
Expand All @@ -148,8 +144,7 @@ public void negativeEpochLengthThrowsException() {
when(genesisConfig.getBftConfigOptions()).thenReturn(configOptions);
when(genesisConfig.getTransitions()).thenReturn(TransitionsConfigOptions.DEFAULT);

assertThatThrownBy(
() -> createProtocolSchedule(new ForkSpec<>(0, configOptions), Collections.emptyList()))
assertThatThrownBy(() -> createProtocolSchedule(List.of(new ForkSpec<>(0, configOptions))))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Epoch length in config must be greater than zero");
}
Expand All @@ -173,8 +168,9 @@ public void blockRewardSpecifiedInTransitionCreatesNewMilestone() {

final ProtocolSchedule schedule =
createProtocolSchedule(
new ForkSpec<>(0, configOptions),
List.of(new ForkSpec<>(transitionBlock, blockRewardTransition)));
List.of(
new ForkSpec<>(0, configOptions),
new ForkSpec<>(transitionBlock, blockRewardTransition)));

assertThat(schedule.streamMilestoneBlocks().count()).isEqualTo(2);
assertThat(schedule.getByBlockNumber(0).getBlockReward())
Expand All @@ -183,8 +179,7 @@ public void blockRewardSpecifiedInTransitionCreatesNewMilestone() {
.isEqualTo(Wei.of(forkBlockReward));
}

private ProtocolSchedule createProtocolSchedule(
final ForkSpec<BftConfigOptions> genesisFork, final List<ForkSpec<BftConfigOptions>> forks) {
private ProtocolSchedule createProtocolSchedule(final List<ForkSpec<BftConfigOptions>> forks) {
final BaseBftProtocolSchedule bftProtocolSchedule =
new BaseBftProtocolSchedule() {
@Override
Expand All @@ -195,7 +190,7 @@ protected Supplier<BlockHeaderValidator.Builder> createBlockHeaderRuleset(
};
return bftProtocolSchedule.createProtocolSchedule(
genesisConfig,
new ForksSchedule<>(genesisFork, forks),
new ForksSchedule<>(forks),
PrivacyParameters.DEFAULT,
false,
bftExtraDataCodec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public void blockModeTransitionsCreatesBlockModeHeaderValidators() {
final ProtocolSchedule schedule =
createProtocolSchedule(
JsonGenesisConfigOptions.fromJsonObject(JsonUtil.createEmptyObjectNode()),
new ForkSpec<>(0, JsonQbftConfigOptions.DEFAULT),
List.of(
new ForkSpec<>(0, JsonQbftConfigOptions.DEFAULT),
new ForkSpec<>(1, arbitraryTransition),
new ForkSpec<>(2, JsonQbftConfigOptions.DEFAULT)));
assertThat(schedule.streamMilestoneBlocks().count()).isEqualTo(3);
Expand All @@ -90,12 +90,10 @@ public void blockModeTransitionsCreatesBlockModeHeaderValidators() {
}

private ProtocolSchedule createProtocolSchedule(
final GenesisConfigOptions genesisConfig,
final ForkSpec<BftConfigOptions> genesisFork,
final List<ForkSpec<BftConfigOptions>> forks) {
final GenesisConfigOptions genesisConfig, final List<ForkSpec<BftConfigOptions>> forks) {
return IbftProtocolSchedule.create(
genesisConfig,
new ForksSchedule<>(genesisFork, forks),
new ForksSchedule<>(forks),
PrivacyParameters.DEFAULT,
false,
bftExtraDataCodec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public Supplier<BlockHeaderValidator.Builder> createBlockHeaderRuleset(
GenesisConfigFile.fromConfig("{\"config\": {\"spuriousDragonBlock\":0}}")
.getConfigOptions();
final ForksSchedule<BftConfigOptions> forksSchedule =
new ForksSchedule<>(new ForkSpec<>(0, configOptions.getBftConfigOptions()), List.of());
new ForksSchedule<>(List.of(new ForkSpec<>(0, configOptions.getBftConfigOptions())));
final ProtocolSchedule protocolSchedule =
bftProtocolSchedule.createProtocolSchedule(
configOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ public void contractModeTransitionsCreatesContractModeHeaderValidators() {
final ProtocolSchedule schedule =
createProtocolSchedule(
JsonGenesisConfigOptions.fromJsonObject(JsonUtil.createEmptyObjectNode()),
new ForkSpec<>(0, qbftConfigOptions),
List.of(new ForkSpec<>(1, arbitraryTransition), new ForkSpec<>(2, contractTransition)));
List.of(
new ForkSpec<>(0, qbftConfigOptions),
new ForkSpec<>(1, arbitraryTransition),
new ForkSpec<>(2, contractTransition)));
assertThat(schedule.streamMilestoneBlocks().count()).isEqualTo(3);
assertThat(validateHeader(schedule, validators, parentHeader, blockHeader, 0)).isTrue();
assertThat(validateHeader(schedule, validators, parentHeader, blockHeader, 1)).isTrue();
Expand All @@ -108,8 +110,8 @@ public void blockModeTransitionsCreatesBlockModeHeaderValidators() {
final ProtocolSchedule schedule =
createProtocolSchedule(
JsonGenesisConfigOptions.fromJsonObject(JsonUtil.createEmptyObjectNode()),
new ForkSpec<>(0, JsonQbftConfigOptions.DEFAULT),
List.of(
new ForkSpec<>(0, JsonQbftConfigOptions.DEFAULT),
new ForkSpec<>(1, arbitraryTransition),
new ForkSpec<>(2, JsonQbftConfigOptions.DEFAULT)));
assertThat(schedule.streamMilestoneBlocks().count()).isEqualTo(3);
Expand All @@ -119,12 +121,10 @@ public void blockModeTransitionsCreatesBlockModeHeaderValidators() {
}

private ProtocolSchedule createProtocolSchedule(
final GenesisConfigOptions genesisConfig,
final ForkSpec<QbftConfigOptions> genesisFork,
final List<ForkSpec<QbftConfigOptions>> forks) {
final GenesisConfigOptions genesisConfig, final List<ForkSpec<QbftConfigOptions>> forks) {
return QbftProtocolSchedule.create(
genesisConfig,
new ForksSchedule<>(genesisFork, forks),
new ForksSchedule<>(forks),
PrivacyParameters.DEFAULT,
false,
bftExtraDataCodec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private Block createEmptyBlock(final long blockNumber, final Hash parentHash) {
@Test
public void usesInitialValidatorProviderWhenNoForks() {
final ForksSchedule<QbftConfigOptions> forksSchedule =
new ForksSchedule<>(createBlockForkSpec(0), List.of());
new ForksSchedule<>(List.of(createBlockForkSpec(0)));

final ForkingValidatorProvider validatorProvider =
new ForkingValidatorProvider(
Expand All @@ -116,7 +116,7 @@ public void usesInitialValidatorProviderWhenNoForks() {
public void migratesFromBlockToContractValidatorProvider() {
final ForksSchedule<QbftConfigOptions> forksSchedule =
new ForksSchedule<>(
createBlockForkSpec(0), List.of(createContractForkSpec(1L, CONTRACT_ADDRESS_1)));
List.of(createBlockForkSpec(0), createContractForkSpec(1L, CONTRACT_ADDRESS_1)));
final ForkingValidatorProvider validatorProvider =
new ForkingValidatorProvider(
blockChain, forksSchedule, blockValidatorProvider, contractValidatorProvider);
Expand All @@ -129,7 +129,7 @@ public void migratesFromBlockToContractValidatorProvider() {
public void migratesFromContractToBlockValidatorProvider() {
final ForksSchedule<QbftConfigOptions> forksSchedule =
new ForksSchedule<>(
createContractForkSpec(0, CONTRACT_ADDRESS_1), List.of(createBlockForkSpec(1)));
List.of(createContractForkSpec(0, CONTRACT_ADDRESS_1), createBlockForkSpec(1)));
final ForkingValidatorProvider validatorProvider =
new ForkingValidatorProvider(
blockChain, forksSchedule, blockValidatorProvider, contractValidatorProvider);
Expand All @@ -147,8 +147,8 @@ public void migratesFromContractToBlockValidatorProvider() {
public void migratesFromContractToContractValidatorProvider() {
final ForksSchedule<QbftConfigOptions> forksSchedule =
new ForksSchedule<>(
createBlockForkSpec(0),
List.of(
createBlockForkSpec(0),
createContractForkSpec(1L, CONTRACT_ADDRESS_1),
createContractForkSpec(2L, CONTRACT_ADDRESS_2)));

Expand All @@ -165,8 +165,10 @@ public void migratesFromContractToContractValidatorProvider() {
public void voteProviderIsDelegatesToHeadFork_whenHeadIsContractFork() {
final ForksSchedule<QbftConfigOptions> forksSchedule =
new ForksSchedule<>(
createBlockForkSpec(0),
List.of(createBlockForkSpec(1), createContractForkSpec(2, CONTRACT_ADDRESS_1)));
List.of(
createBlockForkSpec(0),
createBlockForkSpec(1),
createContractForkSpec(2, CONTRACT_ADDRESS_1)));

final ForkingValidatorProvider validatorProvider =
new ForkingValidatorProvider(
Expand All @@ -181,7 +183,7 @@ public void voteProviderIsDelegatesToHeadFork_whenHeadIsContractFork() {
@Test
public void voteProviderIsDelegatesToHeadFork_whenHeadIsBlockFork() {
final ForksSchedule<QbftConfigOptions> forksSchedule =
new ForksSchedule<>(createBlockForkSpec(0), emptyList());
new ForksSchedule<>(List.of(createBlockForkSpec(0)));

final ForkingValidatorProvider validatorProvider =
new ForkingValidatorProvider(
Expand All @@ -197,8 +199,10 @@ public void voteProviderIsDelegatesToHeadFork_whenHeadIsBlockFork() {
public void getVoteProviderAfterBlock_correctVoteProviderIsResolved() {
final ForksSchedule<QbftConfigOptions> forksSchedule =
new ForksSchedule<>(
createBlockForkSpec(0),
List.of(createBlockForkSpec(1), createContractForkSpec(2, CONTRACT_ADDRESS_1)));
List.of(
createBlockForkSpec(0),
createBlockForkSpec(1),
createContractForkSpec(2, CONTRACT_ADDRESS_1)));
final ForkingValidatorProvider validatorProvider =
new ForkingValidatorProvider(
blockChain, forksSchedule, blockValidatorProvider, contractValidatorProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class TransactionValidatorProviderTest {

@Before
public void setup() {
forksSchedule = new ForksSchedule<>(createContractForkSpec(0L, CONTRACT_ADDRESS), emptyList());
forksSchedule = new ForksSchedule<>(List.of(createContractForkSpec(0L, CONTRACT_ADDRESS)));
genesisBlock = createEmptyBlock(0, Hash.ZERO);
blockChain = createInMemoryBlockchain(genesisBlock);
headerBuilder.extraData(Bytes.wrap(new byte[32]));
Expand Down

0 comments on commit af4221a

Please sign in to comment.