/** * @param Buffer|null $hashStop * @return BlockLocator */ public function getHeadersLocator(Buffer $hashStop = null) { echo 'Produce Headers locator (' . $this->chain->getIndex()->getHeight() . ') ' . PHP_EOL; $height = $this->chain->getIndex()->getHeight(); return $this->getLocator($height, $hashStop); }
/** * We use this to help other nodes sync headers. Identify last common * hash in our chain * * @param Chain $activeChain * @param BlockLocator $locator * @return false|string */ public function findFork(Chain $activeChain, BlockLocator $locator) { if ($this->debug) { echo "db: called findFork\n"; } $hashes = [$activeChain->getIndex()->getHash()]; foreach ($locator->getHashes() as $hash) { $hashes[] = $hash->getHex(); } $placeholders = rtrim(str_repeat('?, ', count($hashes) - 1), ', '); $stmt = $this->dbh->prepare("\n SELECT node.hash\n FROM headerIndex AS node,\n headerIndex AS parent\n WHERE parent.hash = ? AND node.hash in ({$placeholders})\n ORDER BY node.rgt LIMIT 1\n "); if ($stmt->execute($hashes)) { $column = $stmt->fetch(); $stmt->closeCursor(); return $column['hash']; } throw new \RuntimeException('Failed to execute findFork'); }