/** * @param Container $container */ public function register(Container $container) { $headers = $this->node->headers(); $headers->on('headers', function (HeadersBatch $batch) use($container) { /** @var DbInterface $db */ $db = $container['db']; /** @var DebugInterface $debug */ $debug = $container['debug']; if (count($batch->getIndices()) > 0) { $first = $batch->getIndices()[0]; $prevIndex = $db->fetchIndex($first->getHeader()->getPrevBlock()); $versionInfo = $db->findSuperMajorityInfoByHash($prevIndex->getHash()); $forks = new Forks($this->params, $prevIndex, $versionInfo); $first = $forks->toArray(); $changes = []; foreach ($batch->getIndices() as $index) { $forks->next($index); $new = $forks->toArray(); if ($first !== $new) { $changes[] = [$index, $first, $new]; $first = $new; } } foreach ($changes as $change) { /** @var BlockIndexInterface $index */ list($index, $first, $features) = $change; $debug->log('fork.new', ['hash' => $index->getHash()->getHex(), 'height' => $index->getHeight(), 'old' => $first, 'features' => $features]); } } }); }
/** * @param PeerState $state * @param Peer $peer * @param Block $blockMsg */ public function onBlock(PeerState $state, Peer $peer, Block $blockMsg) { $best = $this->node->chain(); $headerIdx = $this->node->headers(); $blockIndex = $this->node->blocks(); $checkSignatures = (bool) $this->config->getItem('config', 'check_signatures', true); $checkSize = (bool) $this->config->getItem('config', 'check_block_size', true); $checkMerkleRoot = (bool) $this->config->getItem('config', 'check_merkle_root', true); try { $index = $blockIndex->accept($blockMsg->getBlock(), $best, $headerIdx, $checkSignatures, $checkSize, $checkMerkleRoot); $this->blockDownload->received($best, $peer, $index->getHash()); } catch (\Exception $e) { $header = $blockMsg->getBlock()->getHeader(); $this->node->emit('event', ['error.onBlock', ['ip' => $peer->getRemoteAddress()->getIp(), 'hash' => $header->getHash()->getHex(), 'error' => $e->getMessage() . PHP_EOL . $e->getTraceAsString()]]); } }
/** * ZmqDebug constructor. * @param NodeInterface $node * @param Context $context */ public function __construct(NodeInterface $node, Context $context) { $this->socket = $context->getSocket(\ZMQ::SOCKET_PUB); $this->socket->bind('tcp://127.0.0.1:5566'); $node->on('event', function ($event, array $params) { $this->log($event, $params); }); $node->headers()->on('tip', [$this, 'logTip']); $node->blocks()->on('block', [$this, 'logBlock']); $node->chains()->on('retarget', [$this, 'logRetarget']); }
/** * @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()]); } }