forked from PegaSysEng/pantheon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add wrapper classes for config section of genesis file (PegaSysEng#208)
* 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
Showing
26 changed files
with
602 additions
and
111 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
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' | ||
} |
37 changes: 37 additions & 0 deletions
37
config/src/main/java/tech/pegasys/pantheon/config/CliqueConfigOptions.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,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); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
config/src/main/java/tech/pegasys/pantheon/config/GenesisConfigOptions.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,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(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
config/src/main/java/tech/pegasys/pantheon/config/IbftConfigOptions.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,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); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
config/src/test/java/tech/pegasys/pantheon/config/CliqueConfigOptionsTest.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,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(); | ||
} | ||
} |
Oops, something went wrong.