Skip to content

Commit

Permalink
support fixed difficulty for keccak mining
Browse files Browse the repository at this point in the history
Signed-off-by: Antoine Toulme <[email protected]>
  • Loading branch information
atoulme committed Feb 12, 2021
1 parent 44aba8e commit eb6b5e8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ public static GenesisConfigFile development() {
}
}

public static GenesisConfigFile ecip1049dev() {
try {
return fromConfig(
Resources.toString(GenesisConfigFile.class.getResource("/ecip1049_dev.json"), UTF_8));
} catch (final IOException e) {
throw new IllegalStateException(e);
}
}

public static GenesisConfigFile fromConfig(final String jsonString) {
return fromConfig(JsonUtil.objectNodeFromString(jsonString, false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ public class FixedDifficultyCalculators {
public static final int DEFAULT_DIFFICULTY = 100;

public static boolean isFixedDifficultyInConfig(final GenesisConfigOptions config) {
return config.getEthashConfigOptions().getFixedDifficulty().isPresent();
return config.getEthashConfigOptions().getFixedDifficulty().isPresent()
|| config.getKeccak256ConfigOptions().getFixedDifficulty().isPresent();
}

public static DifficultyCalculator calculator(final GenesisConfigOptions config) {
long difficulty = config.getEthashConfigOptions().getFixedDifficulty().getAsLong();
long difficulty =
config
.getEthashConfigOptions()
.getFixedDifficulty()
.orElseGet(() -> config.getKeccak256ConfigOptions().getFixedDifficulty().getAsLong());
return (time, parent, context) -> BigInteger.valueOf(difficulty);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,36 @@ public void reportedDifficultyForAllBlocksIsAFixedValue() {
.nextDifficulty(1, parentHeader, null))
.isEqualTo(FixedDifficultyCalculators.DEFAULT_DIFFICULTY);
}

@Test
public void reportedDifficultyForAllBlocksIsAFixedValueKeccak() {

final ProtocolSchedule schedule =
FixedDifficultyProtocolSchedule.create(GenesisConfigFile.ecip1049dev().getConfigOptions());

final BlockHeaderTestFixture headerBuilder = new BlockHeaderTestFixture();

final BlockHeader parentHeader = headerBuilder.number(1).buildHeader();

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

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

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

0 comments on commit eb6b5e8

Please sign in to comment.