Ejemplo n.º 1
0
 /**
  * @param HeadersBatch $batch
  * @return $this
  * @throws \Exception
  */
 public function applyBatch(HeadersBatch $batch)
 {
     $indices = $batch->getIndices();
     if (count($indices) === 0) {
         return $this;
     }
     $this->db->insertHeaderBatch($batch);
     $tip = $batch->getTip();
     foreach ($batch->getIndices() as $index) {
         $tip->updateTip($index);
     }
     $this->emit('tip', [$batch]);
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * @param HeadersBatch $batch
  */
 public function logTip(HeadersBatch $batch)
 {
     $index = $batch->getTip()->getIndex();
     $this->log('tip', ['count' => count($batch->getIndices()), 'tip' => ['hash' => $index->getHash()->getHex(), 'height' => $index->getHeight()]]);
 }
Ejemplo n.º 3
0
 /**
  * @param PeerState $state
  * @param Peer $peer
  * @param HeadersBatch $batch
  */
 public function onHeaders(PeerState $state, Peer $peer, HeadersBatch $batch)
 {
     if (count($batch->getIndices()) < 2000) {
         $this->blockDownload->start($batch->getTip(), $peer);
     }
 }
Ejemplo n.º 4
0
 /**
  * @param HeadersBatch $batch
  * @return bool
  * @throws \Exception
  */
 public function insertHeaderBatch(HeadersBatch $batch)
 {
     $index = $batch->getIndices();
     $segment = $batch->getTip()->getSegment()->getId();
     $this->transaction(function () use($index, $segment) {
         $headerValues = [];
         $headerQuery = [];
         foreach ($index as $c => $i) {
             $headerQuery[] = "(:hash{$c} , :segment{$c} , :height{$c} , :work{$c} ,\n                :version{$c} , :prevBlock{$c} , :merkleRoot{$c} ,\n                :nBits{$c} , :nTimestamp{$c} , :nNonce{$c}  )";
             $headerValues['hash' . $c] = $i->getHash()->getBinary();
             $headerValues['height' . $c] = $i->getHeight();
             $headerValues['segment' . $c] = $segment;
             $headerValues['work' . $c] = $i->getWork();
             $header = $i->getHeader();
             $headerValues['version' . $c] = $header->getVersion();
             $headerValues['prevBlock' . $c] = $header->getPrevBlock()->getBinary();
             $headerValues['merkleRoot' . $c] = $header->getMerkleRoot()->getBinary();
             $headerValues['nBits' . $c] = $header->getBits();
             $headerValues['nTimestamp' . $c] = $header->getTimestamp();
             $headerValues['nNonce' . $c] = $header->getNonce();
         }
         $insertHeaders = $this->dbh->prepare('
           INSERT INTO headerIndex  (hash, segment, height, work, version, prevBlock, merkleRoot, nBits, nTimestamp, nNonce)
           VALUES ' . implode(', ', $headerQuery));
         $insertHeaders->execute($headerValues);
         $lastId = (int) $this->dbh->lastInsertId();
         $count = count($index);
         for ($i = 0; $i < $count; $i++) {
             $rowId = $i + $lastId;
             $indexValues['header_id' . $i] = $rowId;
         }
         return true;
     });
     echo "done\n";
 }