/** * @param ChainStateInterface $chain * @param BlockIndexInterface $prevIndex * @return int|string */ public function getWorkRequired(ChainStateInterface $chain, BlockIndexInterface $prevIndex) { $math = $this->math; if ($math->cmp($math->mod($math->add($prevIndex->getHeight(), 1), $this->params->powRetargetInterval()), 0) !== 0) { // No change in difficulty return $prevIndex->getHeader()->getBits()->getInt(); } // Re-target $heightLastRetarget = $math->sub($prevIndex->getHeight(), $math->sub($this->params->powRetargetInterval(), 1)); $lastTime = $chain->fetchAncestor($heightLastRetarget)->getHeader()->getTimestamp(); return $this->calculateNextWorkRequired($prevIndex, $lastTime); }
/** * @param ChainSegment $segment * @param BlockIndexInterface $index */ public function updateSegment(ChainSegment $segment, BlockIndexInterface $index) { $prevBits = $segment->getLast()->getHeader()->getBits(); $segment->next($index); $this->hashStorage[$segment->getId()][$index->getHash()->getBinary()] = $index->getHeight(); $this->heightStorage[$segment->getId()][$index->getHeight()] = $index->getHash()->getBinary(); if ($index->getHeight() % $this->params->powRetargetInterval() === 0) { $this->emit('retarget', [$segment, $prevBits, $index]); } $this->updateGreatestWork(); }