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

Common protocol schedule config handling #250

Merged
merged 9 commits into from
Nov 13, 2018
3 changes: 3 additions & 0 deletions config/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ dependencies {
testImplementation 'org.mockito:mockito-core'
testImplementation 'junit:junit'
}

configurations { testArtifacts }
artifacts { testSupportArtifacts testSupportJar }
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static GenesisConfigFile fromConfig(final JsonObject config) {
}

public GenesisConfigOptions getConfigOptions() {
return new GenesisConfigOptions(configRoot.getJsonObject("config"));
return new JsonGenesisConfigOptions(configRoot.getJsonObject("config"));
}

public Stream<GenesisAllocation> getAllocations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,76 +15,29 @@
import java.util.OptionalInt;
import java.util.OptionalLong;

import io.vertx.core.json.JsonObject;
public interface GenesisConfigOptions {

public class GenesisConfigOptions {
boolean isEthHash();

private static final String ETHASH_CONFIG_KEY = "ethash";
private static final String IBFT_CONFIG_KEY = "ibft";
private static final String CLIQUE_CONFIG_KEY = "clique";
private final JsonObject configRoot;
boolean isIbft();

GenesisConfigOptions(final JsonObject configRoot) {
this.configRoot = configRoot != null ? configRoot : new JsonObject();
}
boolean isClique();

public boolean isEthHash() {
return configRoot.containsKey(ETHASH_CONFIG_KEY);
}
IbftConfigOptions getIbftConfigOptions();

public boolean isIbft() {
return configRoot.containsKey(IBFT_CONFIG_KEY);
}
CliqueConfigOptions getCliqueConfigOptions();

public boolean isClique() {
return configRoot.containsKey(CLIQUE_CONFIG_KEY);
}
OptionalLong getHomesteadBlockNumber();

public IbftConfigOptions getIbftConfigOptions() {
return isIbft()
? new IbftConfigOptions(configRoot.getJsonObject(IBFT_CONFIG_KEY))
: IbftConfigOptions.DEFAULT;
}
OptionalLong getDaoForkBlock();

public CliqueConfigOptions getCliqueConfigOptions() {
return isClique()
? new CliqueConfigOptions(configRoot.getJsonObject(CLIQUE_CONFIG_KEY))
: CliqueConfigOptions.DEFAULT;
}
OptionalLong getTangerineWhistleBlockNumber();

public OptionalLong getHomesteadBlockNumber() {
return getOptionalLong("homesteadblock");
}
OptionalLong getSpuriousDragonBlockNumber();

public OptionalLong getDaoForkBlock() {
return getOptionalLong("daoforkblock");
}
OptionalLong getByzantiumBlockNumber();

public OptionalLong getTangerineWhistleBlockNumber() {
return getOptionalLong("eip150block");
}
OptionalLong getConstantinopleBlockNumber();

public OptionalLong getSpuriousDragonBlockNumber() {
return getOptionalLong("eip158block");
}

public OptionalLong getByzantiumBlockNumber() {
return getOptionalLong("byzantiumblock");
}

public OptionalLong getConstantinopleBlockNumber() {
return getOptionalLong("constantinopleblock");
}

public OptionalInt getChainId() {
return configRoot.containsKey("chainid")
? OptionalInt.of(configRoot.getInteger("chainid"))
: OptionalInt.empty();
}

private OptionalLong getOptionalLong(final String key) {
return configRoot.containsKey(key)
? OptionalLong.of(configRoot.getLong(key))
: OptionalLong.empty();
}
OptionalInt getChainId();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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.config;

import java.util.OptionalInt;
import java.util.OptionalLong;

import io.vertx.core.json.JsonObject;

public class JsonGenesisConfigOptions implements GenesisConfigOptions {

private static final String ETHASH_CONFIG_KEY = "ethash";
private static final String IBFT_CONFIG_KEY = "ibft";
private static final String CLIQUE_CONFIG_KEY = "clique";
private final JsonObject configRoot;

JsonGenesisConfigOptions(final JsonObject configRoot) {
this.configRoot = configRoot != null ? configRoot : new JsonObject();
}

@Override
public boolean isEthHash() {
return configRoot.containsKey(ETHASH_CONFIG_KEY);
}

@Override
public boolean isIbft() {
return configRoot.containsKey(IBFT_CONFIG_KEY);
}

@Override
public boolean isClique() {
return configRoot.containsKey(CLIQUE_CONFIG_KEY);
}

@Override
public IbftConfigOptions getIbftConfigOptions() {
return isIbft()
? new IbftConfigOptions(configRoot.getJsonObject(IBFT_CONFIG_KEY))
: IbftConfigOptions.DEFAULT;
}

@Override
public CliqueConfigOptions getCliqueConfigOptions() {
return isClique()
? new CliqueConfigOptions(configRoot.getJsonObject(CLIQUE_CONFIG_KEY))
: CliqueConfigOptions.DEFAULT;
}

@Override
public OptionalLong getHomesteadBlockNumber() {
return getOptionalLong("homesteadblock");
}

@Override
public OptionalLong getDaoForkBlock() {
return getOptionalLong("daoforkblock");
}

@Override
public OptionalLong getTangerineWhistleBlockNumber() {
return getOptionalLong("eip150block");
}

@Override
public OptionalLong getSpuriousDragonBlockNumber() {
return getOptionalLong("eip158block");
}

@Override
public OptionalLong getByzantiumBlockNumber() {
return getOptionalLong("byzantiumblock");
}

@Override
public OptionalLong getConstantinopleBlockNumber() {
return getOptionalLong("constantinopleblock");
}

@Override
public OptionalInt getChainId() {
return configRoot.containsKey("chainid")
? OptionalInt.of(configRoot.getInteger("chainid"))
: OptionalInt.empty();
}

private OptionalLong getOptionalLong(final String key) {
return configRoot.containsKey(key)
? OptionalLong.of(configRoot.getLong(key))
: OptionalLong.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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.config;

import java.util.OptionalInt;
import java.util.OptionalLong;

public class StubGenesisConfigOptions implements GenesisConfigOptions {

private OptionalLong homesteadBlockNumber = OptionalLong.empty();
private OptionalLong daoForkBlock = OptionalLong.empty();
private OptionalLong tangerineWhistleBlockNumber = OptionalLong.empty();
private OptionalLong spuriousDragonBlockNumber = OptionalLong.empty();
private OptionalLong byzantiumBlockNumber = OptionalLong.empty();
private OptionalLong constantinopleBlockNumber = OptionalLong.empty();
private OptionalInt chainId = OptionalInt.empty();

@Override
public boolean isEthHash() {
return true;
}

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

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

@Override
public IbftConfigOptions getIbftConfigOptions() {
return IbftConfigOptions.DEFAULT;
}

@Override
public CliqueConfigOptions getCliqueConfigOptions() {
return CliqueConfigOptions.DEFAULT;
}

@Override
public OptionalLong getHomesteadBlockNumber() {
return homesteadBlockNumber;
}

@Override
public OptionalLong getDaoForkBlock() {
return daoForkBlock;
}

@Override
public OptionalLong getTangerineWhistleBlockNumber() {
return tangerineWhistleBlockNumber;
}

@Override
public OptionalLong getSpuriousDragonBlockNumber() {
return spuriousDragonBlockNumber;
}

@Override
public OptionalLong getByzantiumBlockNumber() {
return byzantiumBlockNumber;
}

@Override
public OptionalLong getConstantinopleBlockNumber() {
return constantinopleBlockNumber;
}

@Override
public OptionalInt getChainId() {
return chainId;
}

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

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

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

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

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

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

public StubGenesisConfigOptions chainId(final int chainId) {
this.chainId = OptionalInt.of(chainId);
return this;
}
}
Loading