Esempio n. 1
0
/**
 * class CoinQBlockTreeSqlite3
*/
void CoinQBlockTreeSqlite3::open(const std::string& filename, const Coin::CoinBlockHeader& header, bool bCreateIfNotExists)
{
    uchar_vector genesisHeaderHash = header.getHashLittleEndian();

    std::stringstream sql;
    db.open(filename, bCreateIfNotExists);

    sql << "CREATE TABLE IF NOT EXISTS `block_headers` ("
        <<      "`hash` TEXT UNIQUE NOT NULL,"
        <<      "`in_best_chain` INTEGER NOT NULL,"
        <<      "`height` INTEGER NOT NULL,"
        <<      "`chain_work` TEXT NOT NULL,"
        <<      "`version` INTEGER NOT NULL,"
        <<      "`prev_block_hash` TEXT NOT NULL,"
        <<      "`merkle_root` TEXT NOT NULL,"
        <<      "`timestamp` INTEGER NOT NULL,"
        <<      "`bits` INTEGER NOT NULL,"
        <<      "`nonce` INTEGER NOT NULL,"
        <<      "`time_inserted` INTEGER DEFAULT (strftime('%s', 'now')))";
    db.query(sql.str()); sql.str("");

    SQLite3Tx sqliteTx(db, SQLite3Tx::Type::EXCLUSIVE);
    SQLite3Stmt stmt;
    sql << "SELECT `hash` FROM `block_headers` WHERE `height` = 0";
    stmt.prepare(db, sql.str());
    int count = 0;
    while (stmt.step() == SQLITE_ROW) {
        if (count > 0) {
            throw std::runtime_error("File contains more than one genesis block.");
        }
        uchar_vector hash;
        hash.setHex((char*)stmt.getText(0));
        if (genesisHeaderHash != hash) {
            throw std::runtime_error("Genesis block mismatch.");
        }
        count++;
    }

    if (count == 0) {
        ChainHeader genesisHeader(header, true, 0, header.getWork());

        sql.str("");
        sql << "INSERT INTO `block_headers` (`hash`, `in_best_chain`, `height`, `chain_work`, `version`, `prev_block_hash`, `merkle_root`, `timestamp`, `bits`, `nonce`)"
            << " VALUES ('"
            << genesisHeaderHash.getHex() << "',1,0,'"
            << genesisHeader.chainWork.getDec() << "',"
            << genesisHeader.version << ",'"
            << genesisHeader.prevBlockHash.getHex() << "','"
            << genesisHeader.merkleRoot.getHex() << "',"
            << genesisHeader.timestamp << ","
            << genesisHeader.bits << ","
            << genesisHeader.nonce << ")";
        db.query(sql.str());
        sqliteTx.commit();

        notifyInsert(genesisHeader);
        notifyAddBestChain(genesisHeader);
    }
}
Esempio n. 2
0
void BlockHeader::fromCoinClasses(const Coin::CoinBlockHeader& blockheader, uint32_t height)
{
    hash_ = blockheader.getHashLittleEndian();
    height_ = height;
    version_ = blockheader.version;
    prevhash_ = blockheader.prevBlockHash;
    merkleroot_ = blockheader.merkleRoot;
    timestamp_ = blockheader.timestamp;
    bits_ = blockheader.bits;
    nonce_ = blockheader.nonce;
}