Example #1
0
 /**
  * Given an ancestor height, and a list of hashes in the fork, use this
  * information to compare the work, and if necessary, commit a reorg.
  *
  * @param int $ancestorHeight
  * @param array $forkBlockHashes
  * @return bool
  * @throws \Exception
  */
 public function processFork($ancestorHeight, array $forkBlockHashes)
 {
     $blocks = $this->blocks();
     $index = $this->index();
     /** @var \BitWasp\Bitcoin\Block\BlockHeaderInterface[] $chainHeaders */
     $chainHeaders = [];
     $chainHeight = $index->height()->height();
     for ($i = $ancestorHeight; $i < $chainHeight; $i++) {
         $chainHeaders[] = $blocks->fetch($index->hash()->fetch($i))->getHeader();
     }
     /** @var \BitWasp\Bitcoin\Block\BlockHeaderInterface[] $forkBlocks */
     $forkBlocks = [];
     $forkHeaders = [];
     foreach ($forkBlockHashes as $hash) {
         $block = $blocks->fetch($hash);
         $forkBlocks[] = $block;
         $forkHeaders[] = $block->getHeader();
     }
     // Only recalculate BlockIndex values if fork has greater work than chain blocks since ancestor
     if ($this->difficulty->compareWork($forkHeaders, $chainHeaders) > 0) {
         $index->reorg($ancestorHeight, $forkHeaders);
         return true;
     }
     return false;
 }