/** * @param BufferInterface $blockHash * @param BlockInterface $block * @param BlockSerializerInterface $blockSerializer * @return int */ public function insertBlock(BufferInterface $blockHash, BlockInterface $block, BlockSerializerInterface $blockSerializer) { // Insert the block header ID $this->insertBlockStmt->execute(['hash' => $blockHash->getBinary(), 'block' => $blockSerializer->serialize($block)->getBinary()]); return $this->dbh->lastInsertId(); }
/** * @param BlockInterface $block * @param TransactionSerializerInterface $txSerializer * @param BlockSerializerInterface $blockSerializer * @param bool $checkSize * @param bool $checkMerkleRoot * @return $this * @throws MerkleTreeEmpty */ public function check(BlockInterface $block, TransactionSerializerInterface $txSerializer, BlockSerializerInterface $blockSerializer, $checkSize = true, $checkMerkleRoot = true) { $params = $this->consensus->getParams(); if ($checkMerkleRoot && $this->calcMerkleRoot($block, $txSerializer)->equals($block->getHeader()->getMerkleRoot()) === false) { throw new \RuntimeException('BlockCheck: failed to verify merkle root'); } $txCount = count($block->getTransactions()); if ($checkSize && (0 === $txCount || $blockSerializer->serialize($block)->getSize() > $params->maxBlockSizeBytes())) { throw new \RuntimeException('BlockCheck: Zero transactions, or block exceeds max size'); } // The first transaction is coinbase, and only the first transaction is coinbase. if (!$block->getTransaction(0)->isCoinbase()) { throw new \RuntimeException('BlockCheck: First transaction was not coinbase'); } for ($i = 1; $i < $txCount; $i++) { if ($block->getTransaction($i)->isCoinbase()) { throw new \RuntimeException('BlockCheck: more than one coinbase'); } } $nSigOps = 0; foreach ($block->getTransactions() as $transaction) { $this->checkTransaction($transaction, $txSerializer, $checkSize); $nSigOps += $this->getLegacySigOps($transaction); } if ($nSigOps > $params->getMaxBlockSigOps()) { throw new \RuntimeException('BlockCheck: sigops exceeds maximum allowed'); } return $this; }