Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

NC-2147 Implement Petersburg hardfork #601

Merged
merged 9 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions acceptance-tests/src/test/resources/clique/clique.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"homesteadBlock": 1,
"eip150Block": 2,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"eip155Block": 3,
"eip158Block": 4,
"byzantiumBlock": 5,
"constantinopleBlock": 6,
"constantinopleFixBlock": 7,
"clique": {
"blockperiodseconds": 10,
"epochlength": 30000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ public interface GenesisConfigOptions {

OptionalLong getConstantinopleBlockNumber();

OptionalLong getConstantinopleFixBlockNumber();

OptionalInt getChainId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public OptionalLong getConstantinopleBlockNumber() {
return getOptionalLong("constantinopleblock");
}

@Override
public OptionalLong getConstantinopleFixBlockNumber() {
return getOptionalLong("constantinoplefixblock");
}

@Override
public OptionalInt getChainId() {
return configRoot.containsKey("chainid")
Expand Down
1 change: 1 addition & 0 deletions config/src/main/resources/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"constantinopleFixBlock": 0,
"ethash": {
}
},
Expand Down
1 change: 1 addition & 0 deletions config/src/main/resources/goerli.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"eip160Block":0,
"byzantiumBlock":0,
"constantinopleBlock":0,
"constantinopleFixBlock": 0,
"clique":{
"period":15,
"epoch":30000
Expand Down
2 changes: 2 additions & 0 deletions config/src/main/resources/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"eip155Block": 2675000,
"eip158Block": 2675000,
"byzantiumBlock": 4370000,
"constantinopleBlock": 7280000,
"constantinopleFixBlock": 7280000,
"ethash": {

}
Expand Down
1 change: 1 addition & 0 deletions config/src/main/resources/rinkeby.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"eip155Block": 3,
"eip158Block": 3,
"byzantiumBlock": 1035301,
"constantinopleBlock": 3660663,
"clique": {
"period": 15,
"epoch": 30000
Expand Down
1 change: 1 addition & 0 deletions config/src/main/resources/ropsten.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"eip158Block": 10,
"byzantiumBlock": 1700000,
"constantinopleBlock": 4230000,
"constantinopleFixBlock": 4939394,
"ethash": {
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class StubGenesisConfigOptions implements GenesisConfigOptions {
private OptionalLong spuriousDragonBlockNumber = OptionalLong.empty();
private OptionalLong byzantiumBlockNumber = OptionalLong.empty();
private OptionalLong constantinopleBlockNumber = OptionalLong.empty();
private OptionalLong constantinopleFixBlockNumber = OptionalLong.empty();
private OptionalInt chainId = OptionalInt.empty();

@Override
Expand Down Expand Up @@ -90,6 +91,11 @@ public OptionalLong getConstantinopleBlockNumber() {
return constantinopleBlockNumber;
}

@Override
public OptionalLong getConstantinopleFixBlockNumber() {
return constantinopleFixBlockNumber;
}

@Override
public OptionalInt getChainId() {
return chainId;
Expand Down Expand Up @@ -125,6 +131,11 @@ public StubGenesisConfigOptions constantinopleBlock(final long blockNumber) {
return this;
}

public StubGenesisConfigOptions constantinopleFixBlock(final long blockNumber) {
constantinopleFixBlockNumber = OptionalLong.of(blockNumber);
return this;
}

public StubGenesisConfigOptions chainId(final int chainId) {
this.chainId = OptionalInt.of(chainId);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ public void shouldGetConstantinopleBlockNumber() {
assertThat(config.getConstantinopleBlockNumber()).hasValue(1000);
}

@Test
public void shouldGetConstantinopleFixBlockNumber() {
final GenesisConfigOptions config =
fromConfigOptions(singletonMap("constantinopleFixBlock", 1000));
assertThat(config.getConstantinopleFixBlockNumber()).hasValue(1000);
}

@Test
public void shouldNotReturnEmptyOptionalWhenBlockNumberNotSpecified() {
final GenesisConfigOptions config = fromConfigOptions(emptyMap());
Expand All @@ -110,6 +117,7 @@ public void shouldNotReturnEmptyOptionalWhenBlockNumberNotSpecified() {
assertThat(config.getSpuriousDragonBlockNumber()).isEmpty();
assertThat(config.getByzantiumBlockNumber()).isEmpty();
assertThat(config.getConstantinopleBlockNumber()).isEmpty();
assertThat(config.getConstantinopleFixBlockNumber()).isEmpty();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package tech.pegasys.pantheon.ethereum.vm.operations;

import tech.pegasys.pantheon.ethereum.mainnet.ConstantinopleGasCalculator;
import tech.pegasys.pantheon.ethereum.mainnet.ConstantinopleFixGasCalculator;
import tech.pegasys.pantheon.ethereum.vm.BlockHashLookup;
import tech.pegasys.pantheon.ethereum.vm.MessageFrame;
import tech.pegasys.pantheon.util.bytes.Bytes32;
Expand Down Expand Up @@ -42,7 +42,7 @@ public class BlockHashOperationBenchmark {
@Setup
public void prepare() throws Exception {
operationBenchmarkHelper = OperationBenchmarkHelper.create();
operation = new BlockHashOperation(new ConstantinopleGasCalculator());
operation = new BlockHashOperation(new ConstantinopleFixGasCalculator());
frame = operationBenchmarkHelper.createMessageFrame();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2018 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.
*/
package tech.pegasys.pantheon.ethereum.mainnet;

import tech.pegasys.pantheon.ethereum.core.Account;
import tech.pegasys.pantheon.ethereum.core.Gas;
import tech.pegasys.pantheon.util.uint.UInt256;

/**
* Gas Calculator for Petersberg Hard Fork. Rollback EIP-1283.
*
* <p>Neither {@link TangerineWhistleGasCalculator} nor {@link SpuriousDragonGasCalculator} overrode
* these two methods so {@link FrontierGasCalculator} is the source.
*/
public class ConstantinopleFixGasCalculator extends ConstantinopleGasCalculator {

/** Same as {#link {@link FrontierGasCalculator#STORAGE_SET_GAS_COST} */
private static final Gas STORAGE_SET_GAS_COST = Gas.of(20_000L);
/** Same as {#link {@link FrontierGasCalculator#STORAGE_RESET_GAS_COST} */
private static final Gas STORAGE_RESET_GAS_COST = Gas.of(5_000L);
/** Same as {#link {@link FrontierGasCalculator#STORAGE_RESET_REFUND_AMOUNT} */
private static final Gas STORAGE_RESET_REFUND_AMOUNT = Gas.of(15_000L);

/**
* Same as {#link {@link FrontierGasCalculator#calculateStorageCost(Account, UInt256, UInt256)}
*/
@Override
public Gas calculateStorageCost(
final Account account, final UInt256 key, final UInt256 newValue) {
return !newValue.isZero() && account.getStorageValue(key).isZero()
? STORAGE_SET_GAS_COST
: STORAGE_RESET_GAS_COST;
}

/**
* Same as {#link {@link FrontierGasCalculator#calculateStorageRefundAmount(Account, UInt256,
* UInt256)}
*/
@Override
public Gas calculateStorageRefundAmount(
final Account account, final UInt256 key, final UInt256 newValue) {
return newValue.isZero() && !account.getStorageValue(key).isZero()
? STORAGE_RESET_REFUND_AMOUNT
: Gas.ZERO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ public static ProtocolSpecBuilder<Void> constantinopleDefinition(final int chain
.name("Constantinople");
}

public static ProtocolSpecBuilder<Void> constantinopleFixDefinition(final int chainId) {
return constantinopleDefinition(chainId)
.gasCalculator(ConstantinopleFixGasCalculator::new)
.name("ConstantinopleFix");
}

private static TransactionReceipt frontierTransactionReceiptFactory(
final TransactionProcessor.Result result, final WorldState worldState, final long gasUsed) {
return new TransactionReceipt(worldState.rootHash(), gasUsed, result.getLogs());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public ProtocolSchedule<C> createProtocolSchedule() {
final int chainId = config.getChainId().orElse(defaultChainId);
final MutableProtocolSchedule<C> protocolSchedule = new MutableProtocolSchedule<>(chainId);

validateForkOrdering();

addProtocolSpec(
protocolSchedule, OptionalLong.of(0), MainnetProtocolSpecs.frontierDefinition());
addProtocolSpec(
Expand Down Expand Up @@ -84,6 +86,10 @@ public ProtocolSchedule<C> createProtocolSchedule() {
protocolSchedule,
config.getConstantinopleBlockNumber(),
MainnetProtocolSpecs.constantinopleDefinition(chainId));
addProtocolSpec(
protocolSchedule,
config.getConstantinopleFixBlockNumber(),
MainnetProtocolSpecs.constantinopleFixDefinition(chainId));

return protocolSchedule;
}
Expand All @@ -101,4 +107,34 @@ private void addProtocolSpec(
.privacyParameters(privacyParameters)
.build(protocolSchedule)));
}

private long validateForkOrder(
final String forkName, final OptionalLong thisForkBlock, final long lastForkBlock) {
final long referenceForkBlock = thisForkBlock.orElse(lastForkBlock);
if (lastForkBlock > referenceForkBlock) {
throw new RuntimeException(
String.format(
"Genesis Config Error: '%s' is scheduled for block %d but it must be on or after block %d.",
forkName, thisForkBlock.getAsLong(), lastForkBlock));
}
return referenceForkBlock;
}

private void validateForkOrdering() {
long lastForkBlock = 0;
lastForkBlock = validateForkOrder("Homestead", config.getHomesteadBlockNumber(), lastForkBlock);
lastForkBlock = validateForkOrder("DaoFork", config.getDaoForkBlock(), lastForkBlock);
lastForkBlock =
validateForkOrder(
"TangerineWhistle", config.getTangerineWhistleBlockNumber(), lastForkBlock);
lastForkBlock =
validateForkOrder("SpuriousDragon", config.getSpuriousDragonBlockNumber(), lastForkBlock);
lastForkBlock = validateForkOrder("Byzantium", config.getByzantiumBlockNumber(), lastForkBlock);
lastForkBlock =
validateForkOrder("Constantinople", config.getConstantinopleBlockNumber(), lastForkBlock);
lastForkBlock =
validateForkOrder(
"ConstantinopleFix", config.getConstantinopleFixBlockNumber(), lastForkBlock);
assert (lastForkBlock >= 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public ExecutionContextTestFixture build() {
if (protocolSchedule == null) {
protocolSchedule =
new ProtocolScheduleBuilder<>(
new StubGenesisConfigOptions().constantinopleBlock(0),
new StubGenesisConfigOptions().constantinopleFixBlock(0),
42,
Function.identity(),
PrivacyParameters.noPrivacy())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Expectation expectation(final String milestone) {
final Expectation expectation = expectations.get(milestone);

if (expectation == null) {
throw new IllegalStateException("Expectation for milestone %s not found" + milestone);
throw new IllegalStateException("Expectation for milestone " + milestone + " not found");
}

return expectation;
Expand Down
Loading