/** * {@inheritdoc} * @see \BitWasp\Bitcoin\Chain\DifficultyInterface::getDifficulty() */ public function getDifficulty(Buffer $bits) { $target = $this->math->getCompact($bits); $lowest = $this->math->getCompact($this->lowestBits()); $lowest = $this->math->mul($lowest, $this->math->pow(10, self::DIFF_PRECISION)); $difficulty = str_pad($this->math->div($lowest, $target), self::DIFF_PRECISION + 1, '0', STR_PAD_LEFT); $intPart = substr($difficulty, 0, 0 - self::DIFF_PRECISION); $decPart = substr($difficulty, 0 - self::DIFF_PRECISION, self::DIFF_PRECISION); return $intPart . '.' . $decPart; }
$config = (object) parse_ini_file($configFile); //$currency = Main::getCurrency($currencyName); $db = new MariaInterface(["host" => $config->dbhost, "user" => $config->dbuser, "pass" => $config->dbpass, "port" => $config->dbport, "name" => $config->dbname]); $bitcoind = RpcFactory::bitcoind($config->rpchost, $config->rpcport, $config->rpcuser, $config->rpcpass); $network = NetworkFactory::create($config->magic_byte, $config->magic_p2sh_byte, $config->private_key_byte)->setHDPubByte($config->hd_pub_byte)->setHDPrivByte($config->hd_priv_byte)->setNetMagicBytes($config->net_magic_bytes); Bitcoin::setNetwork($network); $nextBlockHash = $bitcoind->getblockhash($x); do { echo "Block {$x}\n"; $blockhash = $nextBlockHash; $block = $bitcoind->getblock($blockhash); $blockHeader = $block->getHeader(); $blockBits = $blockHeader->getBits(); $blockTime = $blockHeader->getTimestamp(); $nextBlockHash = $blockHeader->getNextBlock(); $bvals = ['isiidsisdss', $blockHeader->getTimestamp(), $blockHeader->getBlockHash(), $block->getBuffer()->getSize(), $x, $blockHeader->getVersion(), $blockHeader->getMerkleRoot(), $blockHeader->getNonce(), $math->getCompact($blockBits), $difficulty->getDifficulty($blockBits), $blockHeader->getPrevBlock(), $nextBlockHash]; $block_id = $db->value('select block_id from blocks where hash = ?', ['s', $blockhash]); if (!$block_id) { $bsql = "insert into blocks " . "(time, " . "hash, " . "size, " . "height, " . "version, " . "merkleroot, " . "nonce, " . "bits, " . "difficulty, " . "previousblockhash, " . "nextblockhash, " . "last_updated " . ") values (from_unixtime(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now())"; $block_id = $db->insert($bsql, $bvals); } else { $bsql = "update blocks set " . "time = ?, hash = ?, size = ?, height = ?, version = ?, " . "merkleroot = ?, nonce = ?, bits = ?, difficulty = ?, " . "previousblockhash = ?, nextblockhash = ?, last_updated = now() " . "where block_id = ?"; $bvals[0] = $bvals[0] . 'i'; $bvals[] = $block_id; $db->update($bsql, $bvals); } $txs = $block->getTransactions(); for ($t = 0; $t < $txs->count(); $t++) { $tx = $txs->getTransaction($t); $txid = $tx->getTransactionId(); //echo "\n","TXID: ",$txid,"\n";