/** * @param BlockIndex $index */ public function add(BlockIndex $index) { if ($index->getHeader()->getPrevBlock() !== $this->getHash($index->getHeight() - 1)->getHex()) { throw new \RuntimeException('ChainCache: New BlockIndex does not refer to last'); } $binary = hex2bin($index->getHash()); $this->hashByHeight[] = $binary; $this->heightByHash[$binary] = $index->getHeight(); }
/** * @param BlockIndex $index */ public function updateTip(BlockIndex $index) { if ($this->index->getHash() !== $index->getHeader()->getPrevBlock()) { throw new \RuntimeException('Header: Header does not extend this chain'); } if ($index->getHeight() - 1 != $this->index->getHeight()) { throw new \RuntimeException('Header: Incorrect chain height'); } $this->chainCache->add($index); $this->index = $index; }
public function testBlockIndex() { $headerHex = '0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c'; $header = BlockHeaderFactory::fromHex($headerHex); $hash = $header->getHash(); $height = 0; $work = 0; $index = new BlockIndex($hash, $height, $work, $header); $this->assertEquals($hash, $index->getHash()); $this->assertEquals($height, $index->getHeight()); $this->assertEquals($work, $index->getWork()); $this->assertSame($header, $index->getHeader()); }
/** * @param Buffer|null $hashStop * @return BlockLocator */ public function getBlockLocator(Buffer $hashStop = null) { echo 'Produce Blocks locator (' . $this->lastBlock->getHeight() . ') ' . PHP_EOL; return $this->getLocator($this->lastBlock->getHeight(), $hashStop); }
/** * @param BlockIndex $prevIndex * @param BlockHeaderInterface $header * @return BlockIndex */ public function makeIndex(BlockIndex $prevIndex, BlockHeaderInterface $header) { return new BlockIndex($header->getHash()->getHex(), $this->math->add($prevIndex->getHeight(), 1), $this->math->add($this->pow->getWork($header->getBits()), $prevIndex->getWork()), $header); }
/** * @param BlockInterface $block * @param BlockIndex $prevBlockIndex * @return bool */ public function checkContextual(BlockInterface $block, BlockIndex $prevBlockIndex) { $newHeight = $prevBlockIndex->getHeight() + 1; $newTime = $block->getHeader()->getTimestamp(); foreach ($block->getTransactions() as $transaction) { if (!$this->checkTransactionIsFinal($transaction, $newHeight, $newTime)) { throw new \RuntimeException('Block contains a non-final transaction'); } } return $this; }