Skip to content

Commit

Permalink
Create ConsensusScheduleBesuControllerBuilder with no migration capab…
Browse files Browse the repository at this point in the history
…ilities (hyperledger#3048)

* Initial implementation of ConsensusScheduleBesuControllerBuilder

Signed-off-by: Lucas Saldanha <[email protected]>
  • Loading branch information
lucassaldanha authored Nov 16, 2021
1 parent f3adc0c commit 8bd41fb
Show file tree
Hide file tree
Showing 10 changed files with 655 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.config.PowAlgorithm;
import org.hyperledger.besu.config.QbftConfigOptions;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
Expand All @@ -36,6 +37,7 @@
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -191,6 +193,10 @@ BesuControllerBuilder fromGenesisConfig(
genesisConfig.getConfigOptions(genesisConfigOverrides);
final BesuControllerBuilder builder;

if (configOptions.isConsensusMigration()) {
return createConsensusScheduleBesuControllerBuilder(genesisConfig, configOptions);
}

if (configOptions.getPowAlgorithm() != PowAlgorithm.UNSUPPORTED) {
builder = new MainnetBesuControllerBuilder();
} else if (configOptions.isIbft2()) {
Expand All @@ -206,5 +212,44 @@ BesuControllerBuilder fromGenesisConfig(
}
return builder.genesisConfigFile(genesisConfig);
}

private BesuControllerBuilder createConsensusScheduleBesuControllerBuilder(
final GenesisConfigFile genesisConfig, final GenesisConfigOptions configOptions) {
final Map<Long, BesuControllerBuilder> besuControllerBuilderSchedule = new HashMap<>();

final BesuControllerBuilder originalControllerBuilder;
if (configOptions.isIbft2()) {
originalControllerBuilder = new IbftBesuControllerBuilder();
} else if (configOptions.isIbftLegacy()) {
originalControllerBuilder = new IbftLegacyBesuControllerBuilder();
} else {
throw new IllegalStateException(
"Invalid genesis migration config. Migration is supported from IBFT (legacy) or IBFT2 to QBFT)");
}
besuControllerBuilderSchedule.put(0L, originalControllerBuilder);

final QbftConfigOptions qbftConfigOptions =
genesisConfig.getConfigOptions().getQbftConfigOptions();
final Long qbftBlock = readQbftStartBlockConfig(qbftConfigOptions);
besuControllerBuilderSchedule.put(qbftBlock, new QbftBesuControllerBuilder());

return new ConsensusScheduleBesuControllerBuilder(besuControllerBuilderSchedule)
.genesisConfigFile(genesisConfig);
}

private Long readQbftStartBlockConfig(final QbftConfigOptions qbftConfigOptions) {
final long startBlock =
qbftConfigOptions
.getStartBlock()
.orElseThrow(
() ->
new IllegalStateException("Missing QBFT startBlock config in genesis file"));

if (startBlock <= 0) {
throw new IllegalStateException("Invalid QBFT startBlock config in genesis file");
}

return startBlock;
}
}
}
Loading

0 comments on commit 8bd41fb

Please sign in to comment.