-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
etcm-193 added possibility to configure target PoW time
- Loading branch information
Showing
16 changed files
with
82 additions
and
20 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
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
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
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
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
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
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
7 changes: 0 additions & 7 deletions
7
src/main/scala/io/iohk/ethereum/consensus/difficulty/ConstantDifficulty.scala
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/main/scala/io/iohk/ethereum/consensus/difficulty/DifficultyCalculator.scala
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 |
---|---|---|
@@ -1,7 +1,18 @@ | ||
package io.iohk.ethereum.consensus.difficulty | ||
|
||
import io.iohk.ethereum.consensus.ethash.difficulty.{ConfiguredDifficultyCalculator, EthashDifficultyCalculator} | ||
import io.iohk.ethereum.domain.BlockHeader | ||
import io.iohk.ethereum.utils.BlockchainConfig | ||
|
||
trait DifficultyCalculator { | ||
def calculateDifficulty(blockNumber: BigInt, blockTimestamp: Long, parent: BlockHeader): BigInt | ||
} | ||
|
||
object DifficultyCalculator { | ||
def apply(blockchainConfig: BlockchainConfig): DifficultyCalculator = { | ||
blockchainConfig.powTargetTime match { | ||
case Some(targetTime) => new ConfiguredDifficultyCalculator(targetTime) | ||
case None => new EthashDifficultyCalculator(blockchainConfig) | ||
} | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
...n/scala/io/iohk/ethereum/consensus/ethash/difficulty/ConfiguredDifficultyCalculator.scala
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 @@ | ||
package io.iohk.ethereum.consensus.ethash.difficulty | ||
|
||
import io.iohk.ethereum.consensus.difficulty.DifficultyCalculator | ||
import io.iohk.ethereum.domain.BlockHeader | ||
|
||
class ConfiguredDifficultyCalculator(powTargetTime: Long) extends DifficultyCalculator { | ||
|
||
val DifficultyBoundDivision: Int = 2048 | ||
val FrontierTimestampDiffLimit: Int = -99 | ||
val MinimumDifficulty: BigInt = 131072 | ||
|
||
/** | ||
* The LowerBoundExpectedRatio (l for abbreviation below) divides the timestamp diff into ranges: | ||
* [0, l) => c = 1, difficulty increases | ||
* [l, 2*l) => c = 0. difficulty stays the same | ||
* ... | ||
* [l*i, l*(i+1) ) => c = 1-i, difficulty decreases | ||
* | ||
* example: | ||
* powTargetTime := 60 seconds | ||
* l := 30 seconds | ||
* [0, 0.5 min) => difficulty increases | ||
* [0.5 min, 1.5 min) => difficulty stays the same (the average should be powTargetTime) | ||
* [1.5 min, +infinity) => difficulty decreases | ||
*/ | ||
val LowerBoundExpectedRatio: Long = powTargetTime / 2 | ||
|
||
def calculateDifficulty(blockNumber: BigInt, blockTimestamp: Long, parentHeader: BlockHeader): BigInt = { | ||
val timestampDiff = blockTimestamp - parentHeader.unixTimestamp | ||
|
||
val x: BigInt = parentHeader.difficulty / DifficultyBoundDivision | ||
val c: BigInt = math.max(1 - (timestampDiff / LowerBoundExpectedRatio), FrontierTimestampDiffLimit) | ||
|
||
MinimumDifficulty.max(parentHeader.difficulty + x * c) | ||
} | ||
} |
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
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
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
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
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