Skip to content

Commit

Permalink
Merge pull request #98 from asolovyov/devel
Browse files Browse the repository at this point in the history
BaseTarget calculation algorithm fixing the blocktimes
  • Loading branch information
catena2w committed Jun 5, 2016
2 parents db2b41b + fa1ba40 commit 3cde852
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ class NxtLikeConsensusModule(AvgDelay: Duration = 5.seconds)
val MaxBlocktimeLimit = normalize(67)
val BaseTargetGamma = normalize(64)

private val AvgDelayInSeconds: Long = AvgDelay.toSeconds

private def normalize(value: Long): Double = value * AvgDelayInSeconds / (60: Double)
private def avgDelayInSeconds: Long = AvgDelay.toSeconds
private def normalize(value: Long): Double = value * avgDelayInSeconds / (60: Double)

override def isValid[TT](block: Block)(implicit transactionModule: TransactionModule[TT]): Boolean = Try {

Expand Down Expand Up @@ -106,7 +105,7 @@ class NxtLikeConsensusModule(AvgDelay: Duration = 5.seconds)
} else Future(None)
}

private def effectiveBalance[TT](account: Account)(implicit transactionModule: TransactionModule[TT]) =
protected def effectiveBalance[TT](account: Account)(implicit transactionModule: TransactionModule[TT]) =
transactionModule.blockStorage.state.asInstanceOf[BalanceSheet]
.balanceWithConfirmations(account.address, EffectiveBalanceDepth)

Expand All @@ -125,14 +124,15 @@ class NxtLikeConsensusModule(AvgDelay: Duration = 5.seconds)
val height = history.heightOf(prevBlock).get
val prevBaseTarget = consensusBlockData(prevBlock).baseTarget
if (height % 2 == 0) {
val blocktimeAverage = history.averageDelay(prevBlock, AvgBlockTimeDepth)
val blocktimeAverage = history.parent(prevBlock, AvgBlockTimeDepth - 1)
.map( b => (timestamp - b.timestampField.value) / AvgBlockTimeDepth)
.getOrElse(timestamp - prevBlock.timestampField.value) / 1000

val baseTarget = (if (blocktimeAverage > AvgDelayInSeconds) {
(prevBaseTarget * Math.min(blocktimeAverage, MaxBlocktimeLimit)) / AvgDelayInSeconds
val baseTarget = (if (blocktimeAverage > avgDelayInSeconds) {
(prevBaseTarget * Math.min(blocktimeAverage, MaxBlocktimeLimit)) / avgDelayInSeconds
} else {
prevBaseTarget - prevBaseTarget * BaseTargetGamma *
(AvgDelayInSeconds - Math.max(blocktimeAverage, MinBlocktimeLimit)) / (AvgDelayInSeconds * 100)
(avgDelayInSeconds - Math.max(blocktimeAverage, MinBlocktimeLimit)) / (avgDelayInSeconds * 100)
}).toLong
bounded(baseTarget, 1, Long.MaxValue).toLong
} else {
Expand Down

0 comments on commit 3cde852

Please sign in to comment.