/** * @param Peer $peer * @param Block $blockMsg */ public function onBlock(Peer $peer, Block $blockMsg) { $best = $this->chain(); $block = $blockMsg->getBlock(); try { $this->blocks->accept($best, $block, $this->headers, $this->utxo); $this->chains->checkTips(); $this->blockDownload->received($best, $peer, $block->getHeader()->getHash()); } catch (\Exception $e) { $header = $block->getHeader(); echo 'Failed to accept block' . PHP_EOL; echo $e->getMessage() . PHP_EOL; if ($best->getChain()->containsHash(Buffer::hex($block->getHeader()->getPrevBlock()))) { if ($header->getPrevBlock() === $best->getLastBlock()->getHash()) { echo $block->getHeader()->getHash()->getHex() . PHP_EOL; echo $block->getHex() . PHP_EOL; echo 'We have prevblockIndex, so this is weird.'; echo $e->getTraceAsString() . PHP_EOL; echo $e->getMessage() . PHP_EOL; } else { echo 'Didn\'t elongate the chain, probably from the future..' . PHP_EOL; } } } }
/** * BetterNode constructor. * @param ConfigProviderInterface $config * @param ParamsInterface $params * @param DbInterface $db */ public function __construct(ConfigProviderInterface $config, ParamsInterface $params, DbInterface $db) { $math = Bitcoin::getMath(); $adapter = Bitcoin::getEcAdapter($math); $this->chains = new ChainContainer($math, $params); $consensus = new Consensus($math, $params); $pow = new ProofOfWork($math, $params); $this->headers = new Index\Headers($db, $adapter, $this->chains, $consensus, $pow); $this->blocks = new Index\Blocks($db, $config, $adapter, $this->chains, $consensus); $this->transactions = new Index\Transactions($db); $genesis = $params->getGenesisBlock(); $this->headers->init($genesis->getHeader()); $this->blocks->init($genesis); $this->db = $db; $segments = $this->db->fetchChainSegments(); foreach ($segments as $segment) { $this->chains->addSegment($segment); } $this->chains->initialize($this->db); }
/** * @param int $minVersion * @param int $startHeight * @param int $nRequired * @param Index\Blocks $blocks * @return bool */ public function isSuperMajority($minVersion, $startHeight, Index\Blocks $blocks, $nRequired) { $nFound = 0; $window = $this->params->majorityWindow(); for ($i = 0; $i < $window && $nFound < $nRequired && ($index = $blocks->fetchByHeight($startHeight - $i)); $i++) { if ($this->math->cmp($index->getHeader()->getVersion(), $minVersion)) { $nFound++; } } return $nFound >= $nRequired; }