Skip to content

Commit

Permalink
BaseTarget calculation algorithm fixing the blocktimes. See details h…
Browse files Browse the repository at this point in the history
  • Loading branch information
asolovyov committed Jun 3, 2016
1 parent 8496360 commit f7f6020
Showing 1 changed file with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,25 @@ import scorex.utils.{NTP, ScorexLogging}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.util.{Failure, Try}


class NxtLikeConsensusModule(AvgDelayInSeconds: Long = 60)
class NxtLikeConsensusModule(AvgDelay: Duration = 5.seconds)
extends LagonakiConsensusModule[NxtLikeConsensusBlockData] with ScorexLogging {

import NxtLikeConsensusModule._

implicit val consensusModule: ConsensusModule[NxtLikeConsensusBlockData] = this

val version = 1: Byte
val version = 2: Byte

val MinBlocktimeLimit = normalize(53)
val MaxBlocktimeLimit = normalize(67)
val BaseTargetGamma = normalize(64)

private val 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 @@ -70,7 +78,6 @@ class NxtLikeConsensusModule(AvgDelayInSeconds: Long = 60)

val eta = (currentTime - lastBlockTime) / 1000


log.debug(s"hit: $h, target: $t, generating ${h < t}, eta $eta, " +
s"account: $account " +
s"account balance: $effBalance"
Expand All @@ -86,6 +93,7 @@ class NxtLikeConsensusModule(AvgDelayInSeconds: Long = 60)

val unconfirmed = transactionModule.packUnconfirmed()
log.debug(s"Build block with ${unconfirmed.asInstanceOf[Seq[Transaction]].size} transactions")
log.debug(s"Block time interval is $eta seconds ")

Future(Some(Block.buildAndSign(version,
currentTime,
Expand All @@ -108,29 +116,27 @@ class NxtLikeConsensusModule(AvgDelayInSeconds: Long = 60)

/**
* BaseTarget calculation algorithm fixing the blocktimes.
*
*/
private def calcBaseTarget[TT](prevBlock: Block, timestamp: Long) (implicit transactionModule: TransactionModule[TT]): Long = {
val height = transactionModule.blockStorage.history.heightOf(prevBlock).get
private def calcBaseTarget[TT](prevBlock: Block, timestamp: Long)
(implicit transactionModule: TransactionModule[TT]): Long = {
val history = transactionModule.blockStorage.history
val height = history.heightOf(prevBlock).get
val prevBaseTarget = consensusBlockData(prevBlock).baseTarget
if (height % 2 == 0) {
val lastBlocks: Seq[Block] = transactionModule.blockStorage.history.lastBlocks(3)
val block = lastBlocks.head
val blocktimeAverage = (timestamp - block.timestampField.value) / lastBlocks.size
val bt =
if (blocktimeAverage > AvgDelayInSeconds) {
(prevBaseTarget * Math.min(blocktimeAverage, MaxBlocktimeLimit)) / AvgDelayInSeconds
}
else {
prevBaseTarget - prevBaseTarget * BaseTargetGamma * (AvgDelayInSeconds - Math.max(blocktimeAverage, MinBlocktimeLimit)) / (AvgDelayInSeconds * 100)
}
if (bt < 0 || bt > MaxBaseTarget2) {
MaxBaseTarget2
} else if (bt < MinBaseTarget) {
MinBaseTarget
val blocktimeAverage = (if (height > AvgBlockTimeDepth) {
(timestamp - history.parent(prevBlock, AvgBlockTimeDepth - 1).get.timestampField.value) / AvgBlockTimeDepth
} else {
bt
}
timestamp - prevBlock.timestampField.value
}) / 1000

val baseTarget = (if (blocktimeAverage > AvgDelayInSeconds) {
(prevBaseTarget * Math.min(blocktimeAverage, MaxBlocktimeLimit)) / AvgDelayInSeconds
} else {
prevBaseTarget -
prevBaseTarget * BaseTargetGamma * (AvgDelayInSeconds - Math.max(blocktimeAverage, MinBlocktimeLimit)) /
(AvgDelayInSeconds * 100)
}).toLong
bounded(baseTarget, 1, Long.MaxValue).toLong
} else {
prevBaseTarget
}
Expand All @@ -143,6 +149,7 @@ class NxtLikeConsensusModule(AvgDelayInSeconds: Long = 60)
val prevBlockTimestamp = prevBlock.timestampField.value

val eta = (timestamp - prevBlockTimestamp) / 1000 //in seconds

BigInt(prevBlockData.baseTarget) * eta * effBalance
}

Expand Down Expand Up @@ -183,12 +190,6 @@ object NxtLikeConsensusModule {
val BaseTargetLength = 8
val GeneratorSignatureLength = 32

val MinBlocktimeLimit = 53
val MaxBlocktimeLimit = 67
val BaseTargetGamma = 64
val InitialBaseTarget = 153722867
val MaxBaseTarget2 = InitialBaseTarget * 50
val MinBaseTarget = InitialBaseTarget * 9 / 10

val EffectiveBalanceDepth = 1440
val EffectiveBalanceDepth: Int = 1440
val AvgBlockTimeDepth: Int = 3
}

0 comments on commit f7f6020

Please sign in to comment.