Example #1
0
 /**
  * @param BufferInterface $hash
  * @return BlockIndexInterface
  */
 public function fetchIndex(BufferInterface $hash)
 {
     if (!$this->view->containsHash($hash)) {
         throw new \RuntimeException('Index by this hash not known');
     }
     return $this->db->fetchIndex($hash);
 }
Example #2
0
 /**
  * @param BlockInterface $genesisBlock
  */
 public function init(BlockInterface $genesisBlock)
 {
     $hash = $genesisBlock->getHeader()->getHash();
     $index = $this->db->fetchIndex($hash);
     try {
         $this->db->fetchBlock($hash);
     } catch (\Exception $e) {
         echo $e->getMessage() . PHP_EOL;
         $this->db->insertBlock($index->getHash(), $genesisBlock, new BlockSerializer(Bitcoin::getMath(), new BlockHeaderSerializer(), new TransactionSerializer()));
     }
 }
Example #3
0
 /**
  * @param BufferInterface $hash
  * @param BlockHeaderInterface $header
  * @return BlockIndexInterface
  * @throws \Exception
  */
 public function accept(BufferInterface $hash, BlockHeaderInterface $header, $haveBlock = false)
 {
     $isTip = $this->chains->isTip($hash);
     if ($isTip instanceof ChainViewInterface) {
         return $isTip->getIndex();
     }
     if ($haveBlock) {
         $hasBlockTip = $this->chains->hasBlockTip($header);
         if ($hasBlockTip instanceof BlockIndexInterface) {
             return $this->getNextIndex($hash, $hasBlockTip, $header);
         }
     }
     if ($this->chains->isKnownHeader($hash)) {
         return $this->db->fetchIndex($hash);
     }
     $batch = $this->prepareBatch([$header]);
     $index = $batch->getIndices()[0];
     $this->applyBatch($batch);
     return $index;
 }
Example #4
0
 /**
  * @param DbInterface $db
  * @throws \Exception
  */
 public function initialize(DbInterface $db)
 {
     $this->db = $db;
     $this->genesis = $db->fetchIndex($this->params->getGenesisBlockHeader()->getHash());
     foreach ($this->segments as $segment) {
         $this->processSegment($db, $segment);
     }
     $this->updateGreatestWork();
 }
Example #5
0
 /**
  * @param BufferInterface $hash
  * @return BlockIndexInterface
  */
 public function fetchIndex(BufferInterface $hash)
 {
     echo __FUNCTION__ . PHP_EOL;
     return $this->db->fetchIndex($hash);
 }