/** * @param BlockIndex $prevIndex * @param $timeFirstBlock * @return int|string */ public function calculateNextWorkRequired(BlockIndex $prevIndex, $timeFirstBlock) { $header = $prevIndex->getHeader(); $math = $this->math; $timespan = $math->sub($header->getTimestamp(), $timeFirstBlock); $lowest = $math->div($this->params->powTargetTimespan(), 4); $highest = $math->mul($this->params->powTargetTimespan(), 4); if ($math->cmp($timespan, $lowest) < 0) { $timespan = $lowest; } if ($math->cmp($timespan, $highest) > 0) { $timespan = $highest; } $target = $math->compact()->set($header->getBits()->getInt()); $limit = $this->math->compact()->set($this->params->powBitsLimit()); $new = bcdiv(bcmul($target, $timespan), $this->params->powTargetTimespan()); if ($math->cmp($new, $limit) > 0) { $new = $limit; } return $math->compact()->read($new, false); }