/**
  * @param Headers $msg
  * @return \BitWasp\Buffertools\Buffer
  */
 public function serialize(Headers $msg)
 {
     $headers = [];
     $null = new Buffer("");
     foreach ($msg->getHeaders() as $header) {
         $temp = new Parser($header->getBuffer());
         $temp->writeBytes(1, $null);
         $headers[] = $temp->getBuffer();
     }
     return $this->getTemplate()->write([$headers]);
 }
 /**
  * @param PeerState $state
  * @param Peer $peer
  * @param Headers $headersMsg
  */
 public function onHeaders(PeerState $state, Peer $peer, Headers $headersMsg)
 {
     $headers = $this->node->headers();
     try {
         $vHeaders = $headersMsg->getHeaders();
         $batch = $headers->prepareBatch($vHeaders);
         $count = count($batch->getIndices());
         if ($count > 0) {
             $headers->applyBatch($batch);
             $view = $batch->getTip();
             $indices = $batch->getIndices();
             $indexLast = end($indices);
             $state->updateBlockAvailability($view, $indexLast->getHash());
             if ($count >= 1999) {
                 $peer->getheaders($view->getHeadersLocator());
             }
         }
         $this->emit('headers', [$state, $peer, $batch]);
     } catch (\Exception $e) {
         $this->debug->log('error.onHeaders', ['error' => $e->getMessage(), 'trace' => $e->getTraceAsString()]);
     }
 }
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testGetHeaderFailure()
 {
     $headers = new Headers([]);
     $headers->getHeader(10);
 }
Beispiel #4
0
 /**
  * @param Peer $peer
  * @param Headers $headers
  */
 public function onHeaders(Peer $peer, Headers $headers)
 {
     $state = $this->chain();
     $vHeaders = $headers->getHeaders();
     $count = count($vHeaders);
     if ($count === 0) {
         return;
     }
     $this->headers->acceptBatch($state, $vHeaders);
     $this->chains->checkTips();
     $last = end($vHeaders);
     $this->peerState->fetch($peer)->updateBlockAvailability($state, $last->getHash());
     if (2000 === $count) {
         $peer->getheaders($state->getHeadersLocator());
     }
     if ($count < 2000) {
         echo 'start download' . PHP_EOL;
         $this->blockDownload->start($state, $peer);
     }
 }