Skip to content

Commit

Permalink
Add wrapper classes for config section of genesis file (PegaSysEng#208)
Browse files Browse the repository at this point in the history
* Introduce classes to wrap JSON config instead of accessing it directly in multiple places.

* Fix discrepancy in how CliqueProtocolSchedule and CliquePantheonController loaded the block period configuration.
  • Loading branch information
ajsutton authored Nov 1, 2018
1 parent d650f46 commit 824b56f
Show file tree
Hide file tree
Showing 26 changed files with 602 additions and 111 deletions.
36 changes: 36 additions & 0 deletions config/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.
*/

apply plugin: 'java-library'

jar {
baseName 'pantheon-config'
manifest {
attributes('Implementation-Title': baseName,
'Implementation-Version': project.version)
}
}

dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'io.vertx:vertx-core'
implementation 'org.apache.logging.log4j:log4j-api'

runtime 'org.apache.logging.log4j:log4j-core'

testImplementation project(':testutil')

testImplementation 'org.assertj:assertj-core'
testImplementation 'org.mockito:mockito-core'
testImplementation 'junit:junit'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 io.vertx.core.json.JsonObject;

public class CliqueConfigOptions {

public static final CliqueConfigOptions DEFAULT = new CliqueConfigOptions(new JsonObject());

private static final long DEFAULT_EPOCH_LENGTH = 30_000;
private static final int DEFAULT_BLOCK_PERIOD_SECONDS = 15;

private final JsonObject cliqueConfigRoot;

public CliqueConfigOptions(final JsonObject cliqueConfigRoot) {
this.cliqueConfigRoot = cliqueConfigRoot;
}

public long getEpochLength() {
return cliqueConfigRoot.getLong("epochLength", DEFAULT_EPOCH_LENGTH);
}

public int getBlockPeriodSeconds() {
return cliqueConfigRoot.getInteger("blockPeriodSeconds", DEFAULT_BLOCK_PERIOD_SECONDS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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 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;

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

public static GenesisConfigOptions fromGenesisConfig(final String genesisConfig) {
return fromGenesisConfig(new JsonObject(genesisConfig));
}

public static GenesisConfigOptions fromGenesisConfig(final JsonObject genesisConfig) {
return new GenesisConfigOptions(genesisConfig.getJsonObject("config"));
}

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

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

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

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

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

public OptionalLong getHomesteadBlockNumber() {
return getOptionalLong("homesteadBlock");
}

public OptionalLong getDaoForkBlock() {
return getOptionalLong("daoForkBlock");
}

public OptionalLong getTangerineWhistleBlockNumber() {
return getOptionalLong("eip150Block");
}

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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 io.vertx.core.json.JsonObject;

public class IbftConfigOptions {

public static final IbftConfigOptions DEFAULT = new IbftConfigOptions(new JsonObject());

private static final long DEFAULT_EPOCH_LENGTH = 30_000;
private static final int DEFAULT_BLOCK_PERIOD_SECONDS = 1;
private static final int DEFAULT_ROUND_EXPIRY_MILLISECONDS = 10000;

private final JsonObject ibftConfigRoot;

public IbftConfigOptions(final JsonObject ibftConfigRoot) {
this.ibftConfigRoot = ibftConfigRoot;
}

public long getEpochLength() {
return ibftConfigRoot.getLong("epochLength", DEFAULT_EPOCH_LENGTH);
}

public int getBlockPeriodSeconds() {
return ibftConfigRoot.getInteger("blockPeriodSeconds", DEFAULT_BLOCK_PERIOD_SECONDS);
}

public int getRequestTimeoutMillis() {
return ibftConfigRoot.getInteger("requestTimeout", DEFAULT_ROUND_EXPIRY_MILLISECONDS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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 static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Map;

import io.vertx.core.json.JsonObject;
import org.junit.Test;

public class CliqueConfigOptionsTest {

private static final long EXPECTED_DEFAULT_EPOCH_LENGTH = 30_000;
private static final int EXPECTED_DEFAULT_BLOCK_PERIOD = 15;

@Test
public void shouldGetEpochLengthFromConfig() {
final CliqueConfigOptions config = fromConfigOptions(singletonMap("epochLength", 10_000));
assertThat(config.getEpochLength()).isEqualTo(10_000);
}

@Test
public void shouldFallbackToDefaultEpochLength() {
final CliqueConfigOptions config = fromConfigOptions(emptyMap());
assertThat(config.getEpochLength()).isEqualTo(EXPECTED_DEFAULT_EPOCH_LENGTH);
}

@Test
public void shouldGetDefaultEpochLengthFromDefaultConfig() {
assertThat(CliqueConfigOptions.DEFAULT.getEpochLength())
.isEqualTo(EXPECTED_DEFAULT_EPOCH_LENGTH);
}

@Test
public void shouldGetBlockPeriodFromConfig() {
final CliqueConfigOptions config = fromConfigOptions(singletonMap("blockPeriodSeconds", 5));
assertThat(config.getBlockPeriodSeconds()).isEqualTo(5);
}

@Test
public void shouldFallbackToDefaultBlockPeriod() {
final CliqueConfigOptions config = fromConfigOptions(emptyMap());
assertThat(config.getBlockPeriodSeconds()).isEqualTo(EXPECTED_DEFAULT_BLOCK_PERIOD);
}

@Test
public void shouldGetDefaultBlockPeriodFromDefaultConfig() {
assertThat(CliqueConfigOptions.DEFAULT.getBlockPeriodSeconds())
.isEqualTo(EXPECTED_DEFAULT_BLOCK_PERIOD);
}

private CliqueConfigOptions fromConfigOptions(final Map<String, Object> cliqueConfigOptions) {
return GenesisConfigOptions.fromGenesisConfig(
new JsonObject(singletonMap("config", singletonMap("clique", cliqueConfigOptions))))
.getCliqueConfigOptions();
}
}
Loading

0 comments on commit 824b56f

Please sign in to comment.