Skip to content

Commit

Permalink
Address code review comments, create a dev network for ecip1049, prep…
Browse files Browse the repository at this point in the history
…are for keccak hasher

Signed-off-by: Antoine Toulme <[email protected]>
  • Loading branch information
atoulme committed Feb 7, 2021
1 parent dc0078f commit cbe4410
Show file tree
Hide file tree
Showing 32 changed files with 389 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void setOptionalFields(
final BlockData blockData,
final GenesisConfigOptions genesisConfig) {
// Some fields can only be configured for ethash
if (genesisConfig.getPowAlgorithm() != PowAlgorithm.NONE) {
if (genesisConfig.getPowAlgorithm() != PowAlgorithm.UNSUPPORTED) {
// For simplicity only set these for ethash. Other consensus algorithms use these fields for
// special purposes or ignore them
miner.setCoinbase(blockData.getCoinbase().orElse(Address.ZERO));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class EthNetworkConfig {
public static final BigInteger RINKEBY_NETWORK_ID = BigInteger.valueOf(4);
public static final BigInteger GOERLI_NETWORK_ID = BigInteger.valueOf(5);
public static final BigInteger DEV_NETWORK_ID = BigInteger.valueOf(2018);
public static final BigInteger ECIP1049_DEV_NETWORK_ID = BigInteger.valueOf(2021);
public static final BigInteger CLASSIC_NETWORK_ID = BigInteger.valueOf(1);
public static final BigInteger KOTTI_NETWORK_ID = BigInteger.valueOf(6);
public static final BigInteger MORDOR_NETWORK_ID = BigInteger.valueOf(7);
Expand All @@ -56,6 +57,7 @@ public class EthNetworkConfig {
private static final String RINKEBY_GENESIS = "/rinkeby.json";
private static final String GOERLI_GENESIS = "/goerli.json";
private static final String DEV_GENESIS = "/dev.json";
private static final String ECIP1049_DEV_GENESIS = "/ecip1049_dev.json";
private static final String CLASSIC_GENESIS = "/classic.json";
private static final String KOTTI_GENESIS = "/kotti.json";
private static final String MORDOR_GENESIS = "/mordor.json";
Expand Down Expand Up @@ -154,6 +156,9 @@ public static EthNetworkConfig getNetworkConfig(final NetworkName networkName) {
case CLASSIC:
return new EthNetworkConfig(
jsonConfig(CLASSIC_GENESIS), CLASSIC_NETWORK_ID, CLASSIC_BOOTSTRAP_NODES, null);
case ECIP1049_DEV:
return new EthNetworkConfig(
jsonConfig(ECIP1049_DEV_GENESIS), ECIP1049_DEV_NETWORK_ID, new ArrayList<>(), null);
case KOTTI:
return new EthNetworkConfig(
jsonConfig(KOTTI_GENESIS), KOTTI_NETWORK_ID, KOTTI_BOOTSTRAP_NODES, null);
Expand Down Expand Up @@ -194,6 +199,8 @@ public static String jsonConfig(final NetworkName network) {
return jsonConfig(GOERLI_GENESIS);
case DEV:
return jsonConfig(DEV_GENESIS);
case ECIP1049_DEV:
return jsonConfig(ECIP1049_DEV_GENESIS);
case CLASSIC:
return jsonConfig(CLASSIC_GENESIS);
case KOTTI:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ public enum NetworkName {
CLASSIC,
KOTTI,
MORDOR,
YOLO_V2
YOLO_V2,
ECIP1049_DEV
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ BesuControllerBuilder fromGenesisConfig(
genesisConfig.getConfigOptions(genesisConfigOverrides);
final BesuControllerBuilder builder;

if (configOptions.getPowAlgorithm() != PowAlgorithm.NONE) {
if (configOptions.getPowAlgorithm() != PowAlgorithm.UNSUPPORTED) {
builder = new MainnetBesuControllerBuilder();
} else if (configOptions.isIbft2()) {
builder = new IbftBesuControllerBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface GenesisConfigOptions {

boolean isEthHash();

boolean isKeccak256();

boolean isIbftLegacy();

boolean isIbft2();
Expand All @@ -43,6 +45,8 @@ public interface GenesisConfigOptions {

EthashConfigOptions getEthashConfigOptions();

Keccak256ConfigOptions getKeccak256ConfigOptions();

OptionalLong getHomesteadBlockNumber();

OptionalLong getDaoForkBlock();
Expand Down Expand Up @@ -173,6 +177,16 @@ public interface GenesisConfigOptions {
*/
OptionalLong getThanosBlockNumber();

/**
* Block number to activate ECIP-1049 on Classic networks. Changes the hashing algorithm to
* keccak-256.
*
* @return block number of ECIP-1049 fork on Classic networks
* @see <a
* href="https://ecips.ethereumclassic.org/ECIPs/ecip-1049">https://ecips.ethereumclassic.org/ECIPs/ecip-1049</a>
*/
OptionalLong getEcip1049BlockNumber();

Optional<BigInteger> getChainId();

OptionalInt getContractSizeLimit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
public class JsonGenesisConfigOptions implements GenesisConfigOptions {

private static final String ETHASH_CONFIG_KEY = "ethash";
private static final String KECCAK256_CONFIG_KEY = "keccak256";
private static final String IBFT_LEGACY_CONFIG_KEY = "ibft";
private static final String IBFT2_CONFIG_KEY = "ibft2";
private static final String QBFT_CONFIG_KEY = "qbft";
Expand Down Expand Up @@ -94,6 +95,8 @@ private JsonGenesisConfigOptions(
public String getConsensusEngine() {
if (isEthHash()) {
return ETHASH_CONFIG_KEY;
} else if (isKeccak256()) {
return KECCAK256_CONFIG_KEY;
} else if (isIbft2()) {
return IBFT2_CONFIG_KEY;
} else if (isIbftLegacy()) {
Expand All @@ -112,6 +115,11 @@ public boolean isEthHash() {
return configRoot.has(ETHASH_CONFIG_KEY);
}

@Override
public boolean isKeccak256() {
return configRoot.has(KECCAK256_CONFIG_KEY);
}

@Override
public boolean isIbftLegacy() {
return configRoot.has(IBFT_LEGACY_CONFIG_KEY);
Expand Down Expand Up @@ -161,6 +169,13 @@ public EthashConfigOptions getEthashConfigOptions() {
.orElse(EthashConfigOptions.DEFAULT);
}

@Override
public Keccak256ConfigOptions getKeccak256ConfigOptions() {
return JsonUtil.getObjectNode(configRoot, KECCAK256_CONFIG_KEY)
.map(Keccak256ConfigOptions::new)
.orElse(Keccak256ConfigOptions.DEFAULT);
}

@Override
public TransitionsConfigOptions getTransitions() {
return transitions;
Expand Down Expand Up @@ -292,6 +307,11 @@ public OptionalLong getThanosBlockNumber() {
return getOptionalLong("thanosblock");
}

@Override
public OptionalLong getEcip1049BlockNumber() {
return getOptionalLong("ecip1049block");
}

@Override
public Optional<BigInteger> getChainId() {
return getOptionalBigInteger("chainid");
Expand Down Expand Up @@ -324,7 +344,9 @@ public OptionalLong getQip714BlockNumber() {

@Override
public PowAlgorithm getPowAlgorithm() {
return isEthHash() ? PowAlgorithm.ETHASH : PowAlgorithm.NONE;
return isEthHash()
? PowAlgorithm.ETHASH
: isKeccak256() ? PowAlgorithm.KECCAK256 : PowAlgorithm.UNSUPPORTED;
}

@Override
Expand Down Expand Up @@ -365,6 +387,9 @@ public Map<String, Object> asMap() {
if (isEthHash()) {
builder.put("ethash", getEthashConfigOptions().asMap());
}
if (isKeccak256()) {
builder.put("keccak256", getKeccak256ConfigOptions().asMap());
}
if (isIbftLegacy()) {
builder.put("ibft", getIbftLegacyConfigOptions().asMap());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.config;

import java.util.Map;
import java.util.OptionalLong;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.ImmutableMap;

public class Keccak256ConfigOptions {

public static final Keccak256ConfigOptions DEFAULT =
new Keccak256ConfigOptions(JsonUtil.createEmptyObjectNode());

private final ObjectNode keccak256ConfigRoot;

Keccak256ConfigOptions(final ObjectNode keccak256ConfigRoot) {
this.keccak256ConfigRoot = keccak256ConfigRoot;
}

public OptionalLong getFixedDifficulty() {
return JsonUtil.getLong(keccak256ConfigRoot, "fixeddifficulty");
}

Map<String, Object> asMap() {
final ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
getFixedDifficulty().ifPresent(l -> builder.put("fixeddifficulty", l));
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
/** An enumeration of supported Proof-of-work algorithms. */
public enum PowAlgorithm {
ETHASH,
NONE;
UNSUPPORTED,
KECCAK256;

public static PowAlgorithm fromString(final String str) {
for (final PowAlgorithm powAlgorithm : PowAlgorithm.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class StubGenesisConfigOptions implements GenesisConfigOptions {
private final OptionalLong aghartaBlockNumber = OptionalLong.empty();
private final OptionalLong phoenixBlockNumber = OptionalLong.empty();
private final OptionalLong thanosBlockNumber = OptionalLong.empty();
private final OptionalLong ecip1049BlockNumber = OptionalLong.empty();
private Optional<BigInteger> chainId = Optional.empty();
private OptionalInt contractSizeLimit = OptionalInt.empty();
private OptionalInt stackSizeLimit = OptionalInt.empty();
Expand All @@ -64,6 +65,11 @@ public boolean isEthHash() {
return true;
}

@Override
public boolean isKeccak256() {
return false;
}

@Override
public boolean isIbftLegacy() {
return false;
Expand Down Expand Up @@ -104,6 +110,11 @@ public EthashConfigOptions getEthashConfigOptions() {
return EthashConfigOptions.DEFAULT;
}

@Override
public Keccak256ConfigOptions getKeccak256ConfigOptions() {
return Keccak256ConfigOptions.DEFAULT;
}

@Override
public OptionalLong getHomesteadBlockNumber() {
return homesteadBlockNumber;
Expand Down Expand Up @@ -205,6 +216,11 @@ public OptionalLong getThanosBlockNumber() {
return thanosBlockNumber;
}

@Override
public OptionalLong getEcip1049BlockNumber() {
return ecip1049BlockNumber;
}

@Override
public OptionalInt getContractSizeLimit() {
return contractSizeLimit;
Expand Down Expand Up @@ -257,6 +273,9 @@ public Map<String, Object> asMap() {
if (isEthHash()) {
builder.put("ethash", getEthashConfigOptions().asMap());
}
if (isKeccak256()) {
builder.put("keccak256", getKeccak256ConfigOptions().asMap());
}
if (isIbftLegacy()) {
builder.put("ibft", getIbftLegacyConfigOptions().asMap());
}
Expand All @@ -283,7 +302,9 @@ public OptionalLong getQip714BlockNumber() {

@Override
public PowAlgorithm getPowAlgorithm() {
return isEthHash() ? PowAlgorithm.ETHASH : PowAlgorithm.NONE;
return isEthHash()
? PowAlgorithm.ETHASH
: isKeccak256() ? PowAlgorithm.KECCAK256 : PowAlgorithm.UNSUPPORTED;
}

@Override
Expand Down
43 changes: 43 additions & 0 deletions config/src/main/resources/ecip1049_dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"config": {
"chainId": 2021,
"homesteadBlock": 0,
"classicForkBlock": 0,
"ecip1015Block": 0,
"diehardBlock": 0,
"gothamBlock": 0,
"ecip1041Block": 0,
"atlantisBlock": 0,
"aghartaBlock": 0,
"phoenixBlock": 0,
"thanosBlock": 0,
"ecip1049Block": 0,
"keccak256": {
"fixeddifficulty": 100
}
},
"nonce": "0x42",
"timestamp": "0x0",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0x1fffffffffffff",
"difficulty": "0x10000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"fe3b557e8fb62b89f4916b721be55ceb828dbd73": {
"privateKey": "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63",
"comment": "private key and this comment are ignored. In a real chain, the private key should NOT be stored",
"balance": "0xad78ebc5ac6200000"
},
"627306090abaB3A6e1400e9345bC60c78a8BEf57": {
"privateKey": "c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3",
"comment": "private key and this comment are ignored. In a real chain, the private key should NOT be stored",
"balance": "90000000000000000000000"
},
"f17f52151EbEF6C7334FAD080c5704D77216b732": {
"privateKey": "ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f",
"comment": "private key and this comment are ignored. In a real chain, the private key should NOT be stored",
"balance": "90000000000000000000000"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,25 @@ public void shouldUseEthHashWhenEthHashInConfig() {
assertThat(config.getConsensusEngine()).isEqualTo("ethash");
}

@Test
public void shouldUseKeccak256WhenKeccak256InConfig() {
final GenesisConfigOptions config = fromConfigOptions(singletonMap("keccak256", emptyMap()));
assertThat(config.isKeccak256()).isTrue();
assertThat(config.getConsensusEngine()).isEqualTo("keccak256");
}

@Test
public void shouldNotUseEthHashIfEthHashNotPresent() {
final GenesisConfigOptions config = fromConfigOptions(emptyMap());
assertThat(config.isEthHash()).isFalse();
}

@Test
public void shouldNotUseKeccak256IfEthHashNotPresent() {
final GenesisConfigOptions config = fromConfigOptions(emptyMap());
assertThat(config.isKeccak256()).isFalse();
}

@Test
public void shouldUseIbftLegacyWhenIbftInConfig() {
final GenesisConfigOptions config = fromConfigOptions(singletonMap("ibft", emptyMap()));
Expand Down Expand Up @@ -195,6 +208,13 @@ public void shouldGetEIP1559BlockNumber() {
}
}

@Test
// TODO ECIP-1049 change for the actual fork name when known
public void shouldGetECIP1049BlockNumber() {
final GenesisConfigOptions config = fromConfigOptions(singletonMap("ecip1049block", 1000));
assertThat(config.getEcip1049BlockNumber()).hasValue(1000);
}

@Test
public void shouldNotReturnEmptyOptionalWhenBlockNumberNotSpecified() {
final GenesisConfigOptions config = fromConfigOptions(emptyMap());
Expand All @@ -208,6 +228,7 @@ public void shouldNotReturnEmptyOptionalWhenBlockNumberNotSpecified() {
assertThat(config.getIstanbulBlockNumber()).isEmpty();
assertThat(config.getMuirGlacierBlockNumber()).isEmpty();
assertThat(config.getBerlinBlockNumber()).isEmpty();
assertThat(config.getEcip1049BlockNumber()).isEmpty();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public static ProtocolSchedule create(
privacyParameters.getGoQuorumPrivacyParameters().isPresent()),
privacyParameters,
isRevertReasonEnabled,
config.isQuorum(),
config.getPowAlgorithm())
config.isQuorum())
.createProtocolSchedule();
}

Expand Down
Loading

0 comments on commit cbe4410

Please sign in to comment.