/** * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $file = getenv('HOME') . '/.bitcoinphp/bitcoin.ini'; $config = new IniConfigProvider(); try { $config->loadFile($file); } catch (\Exception $e) { throw new \RuntimeException('Failed to load config file'); } $db = new Db($config); $db->reset(); }
/** * @param ChainState $state * @param BlockInterface $block * @param Headers $headers * @param UtxoIdx $utxoIdx * @return BlockIndex */ public function accept(ChainState $state, BlockInterface $block, Headers $headers, UtxoIdx $utxoIdx) { $bestBlock = $state->getLastBlock(); if ($bestBlock->getHash() !== $block->getHeader()->getPrevBlock()) { throw new \RuntimeException('Blocks:accept() Block does not extend this chain!'); } $index = $headers->accept($state, $block->getHeader()); $this->blockCheck->check($block)->checkContextual($block, $bestBlock); //$view = $utxoIdx->fetchView($state, $block); $view = $this->db->fetchUtxoView($block); $flagP2sh = $this->consensus->scriptVerifyPayToScriptHash($bestBlock->getHeader()->getTimestamp()); $flags = new Flags($flagP2sh ? InterpreterInterface::VERIFY_P2SH : InterpreterInterface::VERIFY_NONE); $nInputs = 0; $nFees = 0; $nSigOps = 0; $txs = $block->getTransactions(); foreach ($block->getTransactions() as $tx) { $nInputs += count($tx->getInputs()); $nSigOps += $this->blockCheck->getLegacySigOps($tx); if ($nSigOps > $this->consensus->getParams()->getMaxBlockSigOps()) { throw new \RuntimeException('Blocks::accept() - too many sigops'); } if (!$tx->isCoinbase()) { if ($flagP2sh) { $nSigOps = $this->blockCheck->getP2shSigOps($view, $tx); if ($nSigOps > $this->consensus->getParams()->getMaxBlockSigOps()) { throw new \RuntimeException('Blocks::accept() - too many sigops'); } } $fee = $this->math->sub($view->getValueIn($this->math, $tx), $tx->getValueOut()); $nFees = $this->math->add($nFees, $fee); $this->blockCheck->checkInputs($view, $tx, $index->getHeight(), $flags); } } $this->blockCheck->checkCoinbaseSubsidy($txs[0], $nFees, $index->getHeight()); $this->db->insertBlock($block); $state->updateLastBlock($index); return $index; }
/** * @param Peer $peer * @param GetHeaders $getHeaders */ public function onGetHeaders(Peer $peer, GetHeaders $getHeaders) { return; $chain = $this->chain()->getChain(); $locator = $getHeaders->getLocator(); if (count($locator->getHashes()) === 0) { $start = $locator->getHashStop()->getHex(); } else { $start = $this->db->findFork($chain, $locator); } $headers = $this->db->fetchNextHeaders($start); $peer->headers($headers); echo 'Sending from ' . $start . ' + ' . count($headers) . ' headers ' . PHP_EOL; }
/** * @param ChainState $state * @param BlockHeaderInterface[] $headers * @return bool * @throws \Exception */ public function acceptBatch(ChainState $state, array $headers) { $tip = $state->getChain(); $batch = array(); $startIndex = $tip->getIndex(); foreach ($headers as $header) { if ($tip->containsHash($header->getHash())) { continue; } $prevIndex = $tip->getIndex(); if ($prevIndex->getHash() !== $header->getPrevBlock()) { throw new \RuntimeException('Header mismatch, header.prevBlock does not refer to tip'); } $index = $this->headerCheck->check($header)->checkContextual($state, $header)->makeIndex($prevIndex, $header); $tip->updateTip($index); $batch[] = $tip->getIndex(); } // Do a batch update of the chain if (count($batch) > 0) { $this->db->insertIndexBatch($startIndex, $batch); unset($batch); } return true; }