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

Fixed Difficulty calculator #663

Merged
merged 12 commits into from
Jan 29, 2019
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import tech.pegasys.pantheon.cli.PantheonControllerBuilder;
import tech.pegasys.pantheon.controller.KeyPairUtil;
import tech.pegasys.pantheon.controller.PantheonController;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.eth.sync.SynchronizerConfiguration;
import tech.pegasys.pantheon.metrics.MetricsSystem;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;
Expand Down Expand Up @@ -71,6 +72,7 @@ public void startNode(final PantheonNode node) {
.devMode(node.isDevMode())
.nodePrivateKeyFile(KeyPairUtil.getDefaultKeyFile(node.homeDirectory()))
.metricsSystem(noOpMetricsSystem)
.privacyParameters(PrivacyParameters.noPrivacy())
.build();
} catch (final IOException e) {
throw new RuntimeException("Error building PantheonController", e);
Expand Down
25 changes: 25 additions & 0 deletions config/src/main/java/tech/pegasys/pantheon/config/ConfigUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2019 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.OptionalLong;

import io.vertx.core.json.JsonObject;

public class ConfigUtil {
public static OptionalLong getOptionalLong(final JsonObject jsonObject, final String key) {
return jsonObject.containsKey(key)
? OptionalLong.of(jsonObject.getLong(key))
: OptionalLong.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019 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.OptionalLong;

import io.vertx.core.json.JsonObject;

public class EthashConfigOptions {

public static final EthashConfigOptions DEFAULT = new EthashConfigOptions(new JsonObject());;
private final JsonObject ethashConfigRoot;

EthashConfigOptions(final JsonObject ethashConfigRoot) {
this.ethashConfigRoot = ethashConfigRoot;
}

public OptionalLong getFixedDifficulty() {
return ConfigUtil.getOptionalLong(ethashConfigRoot, "fixeddifficulty");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public interface GenesisConfigOptions {

IbftConfigOptions getRevisedIbftConfigOptions();

EthashConfigOptions getEthashConfigOptions();

OptionalLong getHomesteadBlockNumber();

OptionalLong getDaoForkBlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ public CliqueConfigOptions getCliqueConfigOptions() {
: CliqueConfigOptions.DEFAULT;
}

@Override
public EthashConfigOptions getEthashConfigOptions() {
return isEthHash()
? new EthashConfigOptions(configRoot.getJsonObject(ETHASH_CONFIG_KEY))
: EthashConfigOptions.DEFAULT;
}

@Override
public OptionalLong getHomesteadBlockNumber() {
return getOptionalLong("homesteadblock");
Expand Down Expand Up @@ -113,8 +120,6 @@ public OptionalInt getChainId() {
}

private OptionalLong getOptionalLong(final String key) {
return configRoot.containsKey(key)
? OptionalLong.of(configRoot.getLong(key))
: OptionalLong.empty();
return ConfigUtil.getOptionalLong(configRoot, key);
}
}
1 change: 1 addition & 0 deletions config/src/main/resources/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"constantinopleBlock": 0,
"constantinopleFixBlock": 0,
"ethash": {
"fixeddifficulty": 100
}
},
"nonce": "0x42",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public IbftConfigOptions getRevisedIbftConfigOptions() {
return IbftConfigOptions.DEFAULT;
}

@Override
public EthashConfigOptions getEthashConfigOptions() {
return EthashConfigOptions.DEFAULT;
}

@Override
public OptionalLong getHomesteadBlockNumber() {
return homesteadBlockNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.core.LogSeries;
import tech.pegasys.pantheon.ethereum.core.PendingTransactions;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.core.ProcessableBlockHeader;
import tech.pegasys.pantheon.ethereum.core.Transaction;
import tech.pegasys.pantheon.ethereum.core.TransactionReceipt;
import tech.pegasys.pantheon.ethereum.core.TransactionTestFixture;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.core.WorldState;
import tech.pegasys.pantheon.ethereum.core.WorldUpdater;
import tech.pegasys.pantheon.ethereum.development.DevelopmentProtocolSchedule;
import tech.pegasys.pantheon.ethereum.difficulty.fixed.FixedDifficultyProtocolSchedule;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetTransactionProcessor;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetTransactionProcessor.Result;
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule;
Expand Down Expand Up @@ -63,7 +64,8 @@ public class BlockTransactionSelectorTest {
@Test
public void emptyPendingTransactionsResultsInEmptyVettingResult() {
final ProtocolSchedule<Void> protocolSchedule =
DevelopmentProtocolSchedule.create(GenesisConfigFile.DEFAULT.getConfigOptions());
FixedDifficultyProtocolSchedule.create(
GenesisConfigFile.development().getConfigOptions(), PrivacyParameters.noPrivacy());
final Blockchain blockchain = new TestBlockchain();
final TransactionProcessor transactionProcessor =
protocolSchedule.getByBlockNumber(0).getTransactionProcessor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
* 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.development;
package tech.pegasys.pantheon.ethereum.difficulty.fixed;

import tech.pegasys.pantheon.config.GenesisConfigOptions;
import tech.pegasys.pantheon.ethereum.mainnet.DifficultyCalculator;

import java.math.BigInteger;
Expand All @@ -21,12 +22,16 @@
* development (typically) uses CPU based mining, a negligible difficulty ensures tests etc. execute
* quickly.
*/
public class DevelopmentDifficultyCalculators {
public class FixedDifficultyCalculators {

public static final BigInteger MINIMUM_DIFFICULTY = BigInteger.valueOf(500L);
public static final int DEFAULT_DIFFICULTY = 100;

public static DifficultyCalculator<Void> DEVELOPER =
(time, parent, context) -> {
return MINIMUM_DIFFICULTY;
};
public static boolean isFixedDifficultyInConfig(final GenesisConfigOptions config) {
return config.getEthashConfigOptions().getFixedDifficulty().isPresent();
}

public static DifficultyCalculator<Void> calculator(final GenesisConfigOptions config) {
long difficulty = config.getEthashConfigOptions().getFixedDifficulty().getAsLong();
return (time, parent, context) -> BigInteger.valueOf(difficulty);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* 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.development;
package tech.pegasys.pantheon.ethereum.difficulty.fixed;

import static tech.pegasys.pantheon.ethereum.mainnet.MainnetTransactionValidator.NO_CHAIN_ID;

Expand All @@ -20,14 +20,15 @@
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolScheduleBuilder;

/** A ProtocolSchedule which behaves similarly to MainNet, but with a much reduced difficulty. */
public class DevelopmentProtocolSchedule {
public class FixedDifficultyProtocolSchedule {

public static ProtocolSchedule<Void> create(final GenesisConfigOptions config) {
public static ProtocolSchedule<Void> create(
final GenesisConfigOptions config, final PrivacyParameters privacyParameters) {
return new ProtocolScheduleBuilder<>(
config,
NO_CHAIN_ID,
builder -> builder.difficultyCalculator(DevelopmentDifficultyCalculators.DEVELOPER),
PrivacyParameters.noPrivacy())
builder -> builder.difficultyCalculator(FixedDifficultyCalculators.calculator(config)),
privacyParameters)
.createProtocolSchedule();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import tech.pegasys.pantheon.config.GenesisConfigFile;
import tech.pegasys.pantheon.config.GenesisConfigOptions;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.difficulty.fixed.FixedDifficultyCalculators;
import tech.pegasys.pantheon.ethereum.difficulty.fixed.FixedDifficultyProtocolSchedule;

import java.util.function.Function;

Expand All @@ -38,6 +40,9 @@ public static ProtocolSchedule<Void> create() {
*/
public static ProtocolSchedule<Void> fromConfig(
final GenesisConfigOptions config, final PrivacyParameters privacyParameters) {
if (FixedDifficultyCalculators.isFixedDifficultyInConfig(config)) {
return FixedDifficultyProtocolSchedule.create(config, privacyParameters);
}
return new ProtocolScheduleBuilder<>(
config, DEFAULT_CHAIN_ID, Function.identity(), privacyParameters)
.createProtocolSchedule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@
* 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.development;
package tech.pegasys.pantheon.ethereum.difficulty.fixed;

import static org.assertj.core.api.Assertions.assertThat;

import tech.pegasys.pantheon.config.GenesisConfigFile;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule;

import org.junit.Test;

public class DevelopmentProtocolScheduleTest {
public class FixedProtocolScheduleTest {

@Test
public void reportedDifficultyForAllBlocksIsAFixedValue() {

final ProtocolSchedule<Void> schedule =
DevelopmentProtocolSchedule.create(GenesisConfigFile.DEFAULT.getConfigOptions());
FixedDifficultyProtocolSchedule.create(
GenesisConfigFile.development().getConfigOptions(), PrivacyParameters.noPrivacy());

final BlockHeaderTestFixture headerBuilder = new BlockHeaderTestFixture();

Expand All @@ -38,20 +40,20 @@ public void reportedDifficultyForAllBlocksIsAFixedValue() {
.getByBlockNumber(0)
.getDifficultyCalculator()
.nextDifficulty(1, parentHeader, null))
.isEqualTo(DevelopmentDifficultyCalculators.MINIMUM_DIFFICULTY);
.isEqualTo(FixedDifficultyCalculators.DEFAULT_DIFFICULTY);

assertThat(
schedule
.getByBlockNumber(500)
.getDifficultyCalculator()
.nextDifficulty(1, parentHeader, null))
.isEqualTo(DevelopmentDifficultyCalculators.MINIMUM_DIFFICULTY);
.isEqualTo(FixedDifficultyCalculators.DEFAULT_DIFFICULTY);

assertThat(
schedule
.getByBlockNumber(500_000)
.getDifficultyCalculator()
.nextDifficulty(1, parentHeader, null))
.isEqualTo(DevelopmentDifficultyCalculators.MINIMUM_DIFFICULTY);
.isEqualTo(FixedDifficultyCalculators.DEFAULT_DIFFICULTY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
import tech.pegasys.pantheon.config.GenesisConfigFile;
import tech.pegasys.pantheon.ethereum.core.BlockBody;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.core.Transaction;
import tech.pegasys.pantheon.ethereum.development.DevelopmentProtocolSchedule;
import tech.pegasys.pantheon.ethereum.difficulty.fixed.FixedDifficultyProtocolSchedule;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockHashFunction;
import tech.pegasys.pantheon.ethereum.p2p.api.MessageData;
import tech.pegasys.pantheon.ethereum.p2p.wire.RawMessage;
Expand Down Expand Up @@ -65,7 +66,9 @@ public void blockBodiesRoundTrip() throws IOException {
final Iterator<BlockBody> readBodies =
message
.bodies(
DevelopmentProtocolSchedule.create(GenesisConfigFile.DEFAULT.getConfigOptions()))
FixedDifficultyProtocolSchedule.create(
GenesisConfigFile.development().getConfigOptions(),
PrivacyParameters.noPrivacy()))
.iterator();
for (int i = 0; i < 50; ++i) {
Assertions.assertThat(readBodies.next()).isEqualTo(bodies.get(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

import tech.pegasys.pantheon.config.GenesisConfigFile;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.development.DevelopmentProtocolSchedule;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.difficulty.fixed.FixedDifficultyProtocolSchedule;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockHashFunction;
import tech.pegasys.pantheon.ethereum.p2p.api.MessageData;
import tech.pegasys.pantheon.ethereum.p2p.wire.RawMessage;
Expand Down Expand Up @@ -58,7 +59,9 @@ public void blockHeadersRoundTrip() throws IOException {
final BlockHeadersMessage message = BlockHeadersMessage.readFrom(raw);
final Iterator<BlockHeader> readHeaders =
message.getHeaders(
DevelopmentProtocolSchedule.create(GenesisConfigFile.DEFAULT.getConfigOptions()));
FixedDifficultyProtocolSchedule.create(
GenesisConfigFile.development().getConfigOptions(), PrivacyParameters.noPrivacy()));

for (int i = 0; i < 50; ++i) {
Assertions.assertThat(readHeaders.next()).isEqualTo(headers.get(i));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import tech.pegasys.pantheon.config.GenesisConfigFile;
import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.development.DevelopmentProtocolSchedule;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.difficulty.fixed.FixedDifficultyProtocolSchedule;
import tech.pegasys.pantheon.ethereum.eth.manager.ChainState;
import tech.pegasys.pantheon.ethereum.eth.manager.EthProtocolManager;
import tech.pegasys.pantheon.ethereum.eth.manager.EthProtocolManagerTestUtil;
Expand All @@ -44,7 +45,9 @@ public class ChainHeadTrackerTest {
blockchain.getChainHead().getTotalDifficulty(),
0);
private final ProtocolSchedule<Void> protocolSchedule =
DevelopmentProtocolSchedule.create(GenesisConfigFile.DEFAULT.getConfigOptions());
FixedDifficultyProtocolSchedule.create(
GenesisConfigFile.development().getConfigOptions(), PrivacyParameters.noPrivacy());

private final TrailingPeerLimiter trailingPeerLimiter = mock(TrailingPeerLimiter.class);
private final ChainHeadTracker chainHeadTracker =
new ChainHeadTracker(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
import tech.pegasys.pantheon.ethereum.chain.GenesisState;
import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain;
import tech.pegasys.pantheon.ethereum.core.BlockHashFunction;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.core.Transaction;
import tech.pegasys.pantheon.ethereum.core.TransactionPool;
import tech.pegasys.pantheon.ethereum.development.DevelopmentProtocolSchedule;
import tech.pegasys.pantheon.ethereum.difficulty.fixed.FixedDifficultyProtocolSchedule;
import tech.pegasys.pantheon.ethereum.eth.EthProtocol;
import tech.pegasys.pantheon.ethereum.eth.manager.EthContext;
import tech.pegasys.pantheon.ethereum.eth.manager.EthProtocolManager;
Expand Down Expand Up @@ -91,7 +92,9 @@ public TestNode(

final GenesisConfigFile genesisConfigFile = GenesisConfigFile.development();
final ProtocolSchedule<Void> protocolSchedule =
DevelopmentProtocolSchedule.create(genesisConfigFile.getConfigOptions());
FixedDifficultyProtocolSchedule.create(
GenesisConfigFile.development().getConfigOptions(), PrivacyParameters.noPrivacy());

final GenesisState genesisState = GenesisState.fromConfig(genesisConfigFile, protocolSchedule);
final BlockHashFunction blockHashFunction =
ScheduleBasedBlockHashFunction.create(protocolSchedule);
Expand Down
Loading