-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR introduces code to implement Ethereum Classic DieHard Fork support ECIP-1015 Gas cost change ECIP-1010 Delay Difficulty Bomb Explosion Signed-off-by: edwardmack <ed@edwardmack.com>
- Loading branch information
1 parent
55e2475
commit bfa29cd
Showing
8 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...ore/src/main/java/org/hyperledger/besu/ethereum/mainnet/ClassicDifficultyCalculators.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 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 org.hyperledger.besu.util.uint.UInt256; | ||
|
||
import java.math.BigInteger; | ||
|
||
import com.google.common.primitives.Ints; | ||
|
||
public abstract class ClassicDifficultyCalculators { | ||
private static final BigInteger MINIMUM_DIFFICULTY = BigInteger.valueOf(131_072L); | ||
private static final BigInteger DIFFICULTY_BOUND_DIVISOR = BigInteger.valueOf(2_048L); | ||
private static final BigInteger BIGINT_2 = BigInteger.valueOf(2L); | ||
private static final long EXPONENTIAL_DIFF_PERIOD = 100_000L; | ||
private static final long PAUSE_BLOCK = 3_000_000L; | ||
private static final long FIXED_DIFF = PAUSE_BLOCK / EXPONENTIAL_DIFF_PERIOD; | ||
|
||
public static DifficultyCalculator<Void> DIFFICULTY_BOMB_PAUSED = | ||
(time, parent, protocolContext) -> { | ||
final BigInteger parentDifficulty = difficulty(parent.getDifficulty()); | ||
final BigInteger difficulty = | ||
ensureMinimumDifficulty( | ||
BigInteger.valueOf(Math.max(1 - (time - parent.getTimestamp()) / 10, -99L)) | ||
.multiply(parentDifficulty.divide(DIFFICULTY_BOUND_DIVISOR)) | ||
.add(parentDifficulty)); | ||
return adjustForDifficultyPause(FIXED_DIFF, difficulty); | ||
}; | ||
|
||
private static BigInteger adjustForDifficultyPause( | ||
final long periodCount, final BigInteger difficulty) { | ||
return difficulty.add(BIGINT_2.pow(Ints.checkedCast(periodCount - 2))); | ||
} | ||
|
||
private static BigInteger ensureMinimumDifficulty(final BigInteger difficulty) { | ||
return difficulty.compareTo(MINIMUM_DIFFICULTY) < 0 ? MINIMUM_DIFFICULTY : difficulty; | ||
} | ||
|
||
private static BigInteger difficulty(final UInt256 value) { | ||
return new BigInteger(1, value.getBytes().extractArray()); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ClassicProtocolSpecs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 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; | ||
|
||
public class ClassicProtocolSpecs { | ||
|
||
public static ProtocolSpecBuilder<Void> classicRecoveryInitDefinition( | ||
final OptionalInt contractSizeLimit, final OptionalInt configStackSizeLimit) { | ||
return MainnetProtocolSpecs.homesteadDefinition(contractSizeLimit, configStackSizeLimit) | ||
.blockHeaderValidatorBuilder(MainnetBlockHeaderValidator::createClassicValidator) | ||
.name("ClassicRecoveryInit"); | ||
} | ||
|
||
public static ProtocolSpecBuilder<Void> tangerineWhistleDefinition( | ||
final Optional<BigInteger> chainId, | ||
final OptionalInt contractSizeLimit, | ||
final OptionalInt configStackSizeLimit) { | ||
return MainnetProtocolSpecs.homesteadDefinition(contractSizeLimit, configStackSizeLimit) | ||
.gasCalculator(TangerineWhistleGasCalculator::new) | ||
.transactionValidatorBuilder( | ||
gasCalculator -> new MainnetTransactionValidator(gasCalculator, true, chainId)) | ||
.name("ClassicTangerineWhistle"); | ||
} | ||
|
||
public static ProtocolSpecBuilder<Void> dieHardDefinition( | ||
final Optional<BigInteger> chainId, | ||
final OptionalInt configContractSizeLimit, | ||
final OptionalInt configStackSizeLimit) { | ||
return tangerineWhistleDefinition(chainId, OptionalInt.empty(), configStackSizeLimit) | ||
.gasCalculator(DieHardGasCalculator::new) | ||
.difficultyCalculator(ClassicDifficultyCalculators.DIFFICULTY_BOMB_PAUSED) | ||
.name("DieHard"); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/DieHardGasCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 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 org.hyperledger.besu.ethereum.core.Gas; | ||
|
||
public class DieHardGasCalculator extends TangerineWhistleGasCalculator { | ||
private static final Gas EXP_OPERATION_BYTE_GAS_COST = Gas.of(50L); | ||
|
||
@Override | ||
protected Gas expOperationByteGasCost() { | ||
return EXP_OPERATION_BYTE_GAS_COST; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters