/** * @param BlockHeaderInterface $header * @return Chain */ public function findTipForNext(BlockHeaderInterface $header) { foreach ($this->states as $state) { $tTip = $state->getChain(); $tipHash = $tTip->getIndex()->getHash(); if ($header->getPrevBlock() == $tipHash) { $tip = $tTip; } } if (!isset($tip)) { throw new \RuntimeException('No tip found for this Header'); } return $tip; }
/** * @param BlockHeaderInterface $header * @return BlockIndexInterface|bool */ public function hasBlockTip(BlockHeaderInterface $header) { echo "CheckHashBlockTip - " . count($this->segments) . PHP_EOL; foreach ($this->segments as $segment) { /** @var BlockIndexInterface $segBlock */ $segBlock = $this->segmentBlock->offsetGet($segment); if ($header->getPrevBlock()->equals($segBlock->getHash())) { return $segBlock; } } return false; }
/** * @param BlockHeaderInterface $header * @return \BitWasp\Buffertools\Buffer */ public function serialize(BlockHeaderInterface $header) { return $this->getTemplate()->write([$header->getVersion(), Buffer::hex($header->getPrevBlock()), Buffer::hex($header->getMerkleRoot()), $header->getTimestamp(), $header->getBits(), $header->getNonce()]); }
/** * Add the block to the cache and index, commiting the utxos also. * Will fail if it doesn't elongate the current chain * * @param BlockHeaderInterface $header * @throws BlockPrevNotFound */ public function add(BlockHeaderInterface $header) { if ($this->chainTip()->getBlockHash() !== $header->getPrevBlock()) { throw new BlockPrevNotFound('Block does not elongate the current chain'); } $this->storeBlock($header)->updateProofOfWork(); }
/** * Creates the Genesis block index * @param BlockHeaderInterface $header * @return bool */ public function createIndexGenesis(BlockHeaderInterface $header) { $stmtHeader = $this->dbh->prepare('INSERT INTO headerIndex ( hash, segment, height, work, version, prevBlock, merkleRoot, nBits, nTimestamp, nNonce ) VALUES ( :hash, :segment, :height, :work, :version, :prevBlock, :merkleRoot, :nBits, :nTimestamp, :nNonce ) '); if ($stmtHeader->execute(array('hash' => $header->getHash()->getBinary(), 'segment' => 0, 'height' => 0, 'work' => 0, 'version' => $header->getVersion(), 'prevBlock' => $header->getPrevBlock()->getBinary(), 'merkleRoot' => $header->getMerkleRoot()->getBinary(), 'nBits' => $header->getBits(), 'nTimestamp' => $header->getTimestamp(), 'nNonce' => $header->getNonce()))) { return true; } throw new \RuntimeException('Failed to update insert Genesis block index!'); }