Skip to content

Commit

Permalink
Allow for switching ProtocolSchedule rules outside of Milestones (#1962)
Browse files Browse the repository at this point in the history
This is the first step in supporting switchable consensus mechanisms.

Specifically this allows additional protocol specs to be inserted to the protocol schedule at milestones other than that explicitly specified in the genesis config.

Signed-off-by: Trent Mohay <[email protected]>
  • Loading branch information
Trent Mohay authored Mar 4, 2021
1 parent eddd35f commit a729dbd
Show file tree
Hide file tree
Showing 13 changed files with 592 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSpecs;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpecAdapters;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpecBuilder;

import java.math.BigInteger;
Expand Down Expand Up @@ -64,14 +65,16 @@ public static ProtocolSchedule create(
return new ProtocolScheduleBuilder(
config,
DEFAULT_CHAIN_ID,
builder ->
applyCliqueSpecificModifications(
epochManager,
cliqueConfig.getBlockPeriodSeconds(),
localNodeAddress,
builder,
eip1559,
privacyParameters.getGoQuorumPrivacyParameters().isPresent()),
ProtocolSpecAdapters.create(
0,
builder ->
applyCliqueSpecificModifications(
epochManager,
cliqueConfig.getBlockPeriodSeconds(),
localNodeAddress,
builder,
eip1559,
privacyParameters.getGoQuorumPrivacyParameters().isPresent())),
privacyParameters,
isRevertReasonEnabled,
config.isQuorum())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSpecs;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpecAdapters;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpecBuilder;

import java.math.BigInteger;
Expand All @@ -44,9 +45,14 @@ public static ProtocolSchedule create(
return new ProtocolScheduleBuilder(
config,
DEFAULT_CHAIN_ID,
builder ->
applyBftChanges(
config.getBftConfigOptions(), builder, config.isQuorum(), blockHeaderRuleset),
ProtocolSpecAdapters.create(
0,
builder ->
applyBftChanges(
config.getBftConfigOptions(),
builder,
config.isQuorum(),
blockHeaderRuleset)),
privacyParameters,
isRevertReasonEnabled,
config.isQuorum())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSpecs;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpecAdapters;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpecBuilder;

import java.math.BigInteger;
Expand All @@ -44,9 +45,11 @@ public static ProtocolSchedule create(
return new ProtocolScheduleBuilder(
config,
DEFAULT_CHAIN_ID,
builder ->
applyIbftChanges(
blockPeriod, builder, config.isQuorum(), ibftConfig.getCeil2Nby3Block()),
ProtocolSpecAdapters.create(
0,
builder ->
applyIbftChanges(
blockPeriod, builder, config.isQuorum(), ibftConfig.getCeil2Nby3Block())),
privacyParameters,
isRevertReasonEnabled,
config.isQuorum())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.hyperledger.besu.ethereum.mainnet.EthHashSolver;
import org.hyperledger.besu.ethereum.mainnet.EthHasher;
import org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpecAdapters;
import org.hyperledger.besu.ethereum.mainnet.ValidationTestUtils;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.plugin.services.MetricsSystem;
Expand Down Expand Up @@ -71,7 +72,7 @@ public void createMainnetBlock1() throws IOException {
new ProtocolScheduleBuilder(
genesisConfigOptions,
BigInteger.valueOf(42),
Function.identity(),
ProtocolSpecAdapters.create(0, Function.identity()),
PrivacyParameters.DEFAULT,
false,
genesisConfigOptions.isQuorum())
Expand Down Expand Up @@ -130,7 +131,7 @@ public void createMainnetBlock1_fixedDifficulty1() {
new ProtocolScheduleBuilder(
genesisConfigOptions,
BigInteger.valueOf(42),
Function.identity(),
ProtocolSpecAdapters.create(0, Function.identity()),
PrivacyParameters.DEFAULT,
false,
genesisConfigOptions.isQuorum())
Expand Down Expand Up @@ -184,7 +185,7 @@ public void rewardBeneficiary_zeroReward_skipZeroRewardsFalse() {
new ProtocolScheduleBuilder(
genesisConfigOptions,
BigInteger.valueOf(42),
Function.identity(),
ProtocolSpecAdapters.create(0, Function.identity()),
PrivacyParameters.DEFAULT,
false,
genesisConfigOptions.isQuorum())
Expand Down Expand Up @@ -254,7 +255,7 @@ public void rewardBeneficiary_zeroReward_skipZeroRewardsTrue() {
new ProtocolScheduleBuilder(
genesisConfigOptions,
BigInteger.valueOf(42),
Function.identity(),
ProtocolSpecAdapters.create(0, Function.identity()),
PrivacyParameters.DEFAULT,
false,
genesisConfigOptions.isQuorum())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpecAdapters;

/** A ProtocolSchedule which behaves similarly to MainNet, but with a much reduced difficulty. */
public class FixedDifficultyProtocolSchedule {
Expand All @@ -28,7 +29,10 @@ public static ProtocolSchedule create(
final boolean isRevertReasonEnabled) {
return new ProtocolScheduleBuilder(
config,
builder -> builder.difficultyCalculator(FixedDifficultyCalculators.calculator(config)),
ProtocolSpecAdapters.create(
0,
builder ->
builder.difficultyCalculator(FixedDifficultyCalculators.calculator(config))),
privacyParameters,
isRevertReasonEnabled,
config.isQuorum())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static ProtocolSchedule fromConfig(
return new ProtocolScheduleBuilder(
config,
DEFAULT_CHAIN_ID,
Function.identity(),
ProtocolSpecAdapters.create(0, Function.identity()),
privacyParameters,
isRevertReasonEnabled,
config.isQuorum())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Copyright 2020 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.mainnet;

import java.math.BigInteger;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;

public class MainnetProtocolSpecFactory {

private final Optional<BigInteger> chainId;
private final OptionalInt contractSizeLimit;
private final OptionalInt evmStackSize;
private final boolean isRevertReasonEnabled;
private final boolean quorumCompatibilityMode;
private final OptionalLong ecip1017EraRounds;

public MainnetProtocolSpecFactory(
final Optional<BigInteger> chainId,
final OptionalInt contractSizeLimit,
final OptionalInt evmStackSize,
final boolean isRevertReasonEnabled,
final boolean quorumCompatibilityMode,
final OptionalLong ecip1017EraRounds) {
this.chainId = chainId;
this.contractSizeLimit = contractSizeLimit;
this.evmStackSize = evmStackSize;
this.isRevertReasonEnabled = isRevertReasonEnabled;
this.quorumCompatibilityMode = quorumCompatibilityMode;
this.ecip1017EraRounds = ecip1017EraRounds;
}

public ProtocolSpecBuilder frontierDefinition() {
return MainnetProtocolSpecs.frontierDefinition(
contractSizeLimit, evmStackSize, quorumCompatibilityMode);
}

public ProtocolSpecBuilder homesteadDefinition() {
return MainnetProtocolSpecs.homesteadDefinition(
contractSizeLimit, evmStackSize, quorumCompatibilityMode);
}

public ProtocolSpecBuilder daoRecoveryInitDefinition() {
return MainnetProtocolSpecs.daoRecoveryInitDefinition(
contractSizeLimit, evmStackSize, quorumCompatibilityMode);
}

public ProtocolSpecBuilder daoRecoveryTransitionDefinition() {
return MainnetProtocolSpecs.daoRecoveryTransitionDefinition(
contractSizeLimit, evmStackSize, quorumCompatibilityMode);
}

public ProtocolSpecBuilder tangerineWhistleDefinition() {
return MainnetProtocolSpecs.tangerineWhistleDefinition(
contractSizeLimit, evmStackSize, quorumCompatibilityMode);
}

public ProtocolSpecBuilder spuriousDragonDefinition() {
return MainnetProtocolSpecs.spuriousDragonDefinition(
chainId, contractSizeLimit, evmStackSize, quorumCompatibilityMode);
}

public ProtocolSpecBuilder byzantiumDefinition() {
return MainnetProtocolSpecs.byzantiumDefinition(
chainId, contractSizeLimit, evmStackSize, isRevertReasonEnabled, quorumCompatibilityMode);
}

public ProtocolSpecBuilder constantinopleDefinition() {
return MainnetProtocolSpecs.constantinopleDefinition(
chainId, contractSizeLimit, evmStackSize, isRevertReasonEnabled, quorumCompatibilityMode);
}

public ProtocolSpecBuilder petersburgDefinition() {
return MainnetProtocolSpecs.petersburgDefinition(
chainId, contractSizeLimit, evmStackSize, isRevertReasonEnabled, quorumCompatibilityMode);
}

public ProtocolSpecBuilder istanbulDefinition() {
return MainnetProtocolSpecs.istanbulDefinition(
chainId, contractSizeLimit, evmStackSize, isRevertReasonEnabled, quorumCompatibilityMode);
}

public ProtocolSpecBuilder muirGlacierDefinition() {
return MainnetProtocolSpecs.muirGlacierDefinition(
chainId, contractSizeLimit, evmStackSize, isRevertReasonEnabled, quorumCompatibilityMode);
}

public ProtocolSpecBuilder berlinDefinition() {
return MainnetProtocolSpecs.berlinDefinition(
chainId, contractSizeLimit, evmStackSize, isRevertReasonEnabled, quorumCompatibilityMode);
}

////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
// Classic Protocol Specs
public ProtocolSpecBuilder dieHardDefinition() {
return ClassicProtocolSpecs.dieHardDefinition(
chainId, contractSizeLimit, evmStackSize, quorumCompatibilityMode);
}

public ProtocolSpecBuilder gothamDefinition() {
return ClassicProtocolSpecs.gothamDefinition(
chainId, contractSizeLimit, evmStackSize, ecip1017EraRounds, quorumCompatibilityMode);
}

public ProtocolSpecBuilder defuseDifficultyBombDefinition() {
return ClassicProtocolSpecs.defuseDifficultyBombDefinition(
chainId, contractSizeLimit, evmStackSize, ecip1017EraRounds, quorumCompatibilityMode);
}

public ProtocolSpecBuilder atlantisDefinition() {
return ClassicProtocolSpecs.atlantisDefinition(
chainId,
contractSizeLimit,
evmStackSize,
isRevertReasonEnabled,
ecip1017EraRounds,
quorumCompatibilityMode);
}

public ProtocolSpecBuilder aghartaDefinition() {
return ClassicProtocolSpecs.aghartaDefinition(
chainId,
contractSizeLimit,
evmStackSize,
isRevertReasonEnabled,
ecip1017EraRounds,
quorumCompatibilityMode);
}

public ProtocolSpecBuilder phoenixDefinition() {
return ClassicProtocolSpecs.phoenixDefinition(
chainId,
contractSizeLimit,
evmStackSize,
isRevertReasonEnabled,
ecip1017EraRounds,
quorumCompatibilityMode);
}

public ProtocolSpecBuilder thanosDefinition() {
return ClassicProtocolSpecs.thanosDefinition(
chainId,
contractSizeLimit,
evmStackSize,
isRevertReasonEnabled,
ecip1017EraRounds,
quorumCompatibilityMode);
}
}
Loading

0 comments on commit a729dbd

Please sign in to comment.