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 742c34f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.hyperledger.besu.config.EthashConfigOptions;
import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.config.Keccak256ConfigOptions;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.blockcreation.GasLimitCalculator;
import org.hyperledger.besu.ethereum.core.Hash;
Expand Down Expand Up @@ -66,6 +67,7 @@ public class BesuControllerBuilderTest {
@Mock GenesisConfigFile genesisConfigFile;
@Mock GenesisConfigOptions genesisConfigOptions;
@Mock EthashConfigOptions ethashConfigOptions;
@Mock Keccak256ConfigOptions keccak256ConfigOptions;
@Mock SynchronizerConfiguration synchronizerConfiguration;
@Mock EthProtocolConfiguration ethProtocolConfiguration;
@Mock MiningParameters miningParameters;
Expand Down Expand Up @@ -95,6 +97,8 @@ public void setup() {
when(genesisConfigOptions.getThanosBlockNumber()).thenReturn(OptionalLong.empty());
when(genesisConfigOptions.getEthashConfigOptions()).thenReturn(ethashConfigOptions);
when(ethashConfigOptions.getFixedDifficulty()).thenReturn(OptionalLong.empty());
when(genesisConfigOptions.getKeccak256ConfigOptions()).thenReturn(keccak256ConfigOptions);
when(keccak256ConfigOptions.getFixedDifficulty()).thenReturn(OptionalLong.empty());
when(storageProvider.getStorageBySegmentIdentifier(any()))
.thenReturn(new InMemoryKeyValueStorage());
when(storageProvider.createBlockchainStorage(any()))
Expand Down
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 742c34f

Please sign in to comment.