-
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
17 changed files
with
93 additions
and
28 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.
15 changes: 15 additions & 0 deletions
15
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,22 @@ | ||
package io.iohk.ethereum.consensus.difficulty | ||
|
||
import io.iohk.ethereum.consensus.ethash.difficulty.{TargetTimeDifficultyCalculator, 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 TargetTimeDifficultyCalculator(targetTime) | ||
case None => new EthashDifficultyCalculator(blockchainConfig) | ||
} | ||
} | ||
|
||
val DifficultyBoundDivision: Int = 2048 | ||
val FrontierTimestampDiffLimit: Int = -99 | ||
val MinimumDifficulty: BigInt = 131072 | ||
} |
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
34 changes: 34 additions & 0 deletions
34
...n/scala/io/iohk/ethereum/consensus/ethash/difficulty/TargetTimeDifficultyCalculator.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,34 @@ | ||
package io.iohk.ethereum.consensus.ethash.difficulty | ||
|
||
import io.iohk.ethereum.consensus.difficulty.DifficultyCalculator | ||
import io.iohk.ethereum.domain.BlockHeader | ||
|
||
class TargetTimeDifficultyCalculator(powTargetTime: Long) extends DifficultyCalculator { | ||
|
||
import DifficultyCalculator._ | ||
|
||
/** | ||
* 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 := 45 seconds | ||
* l := 30 seconds | ||
* [0, 0.5 min) => difficulty increases | ||
* [0.5 min, 1 min) => difficulty stays the same (the average should be powTargetTime) | ||
* [1 min, +infinity) => difficulty decreases | ||
*/ | ||
private val lowerBoundExpectedRatio: Long = (powTargetTime / 1.5).toLong | ||
|
||
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