/** * @param BufferInterface $hash * @param BlockHeaderInterface $header * @param bool $checkPow * @return $this */ public function check(BufferInterface $hash, BlockHeaderInterface $header, $checkPow = true) { try { if ($checkPow) { $this->pow->check($hash, $header->getBits()); } } catch (\Exception $e) { throw new \RuntimeException('Headers::check() - failed validating header proof-of-work'); } return $this; }
/** * Process a block against the given state of the chain. * @param BlockInterface $block * @return bool */ public function process(BlockInterface $block) { // Ignore the genesis block $header = $block->getHeader(); $hash = $header->getBlockHash(); if ($hash === $this->genesis->getHeader()->getBlockHash()) { return true; } if ($this->index()->height()->contains($hash)) { return true; } try { // Attempt to add it to the chain $this->add($block); $this->pow->checkHeader($header); $result = true; } catch (BlockPrevNotFound $e) { // If it fails because it doesn't elongate the chain, process it as an orphan. // Result will be determined $result = $this->processOrphan($block); } catch (BlockPowError $e) { $result = false; // Invalid block. } return $result; }
/** * @param BufferInterface $hash * @param BlockIndexInterface $prevIndex * @param BlockHeaderInterface $header * @return BlockIndex */ public function getNextIndex(BufferInterface $hash, BlockIndexInterface $prevIndex, BlockHeaderInterface $header) { return new BlockIndex($hash, $prevIndex->getHeight() + 1, $this->math->toString($this->math->add($this->proofOfWork->getWork($header->getBits()), gmp_init($prevIndex->getWork()))), $header); }