Exemplo n.º 1
0
  virtual int init(
    int         argc,
    const char *argv[]
    ) {
    txID     = -1;
    blkID    = 0;
    inputID  = 0;
    outputID = 0;

    static uint64_t sz = 32 * 1000 * 1000;
    outputMap.setEmptyKey(empty);
    outputMap.resize(sz);

    optparse::Values& values = parser.parse_args(argc, argv);
    lastBlock = values.get("atBlock").asUInt64();

    if (!file_exists("../blockchain/blockchain.sqlite"))
    {
      firstBlock = 0;
    }
    else
    {
      sqlite3pp::database db("../blockchain/blockchain.sqlite");
      sqlite3pp::query    qry(db, "SELECT MAX(block_id) FROM blocks");

      sqlite3pp::query::iterator i = qry.begin();

      firstBlock = (uint64_t)atoi((*i).get<char const *>(0)) + 1;

      info("Resuming from block %" PRIu64 ".\n", firstBlock);
    }

    info("Dumping the blockchain...");

    txFile = fopen("tx.txt", "w");

    if (!txFile) sysErrFatal("couldn't open file txs.txt for writing\n");

    blockFile = fopen("blocks.txt", "w");

    if (!blockFile) sysErrFatal("couldn't open file blocks.txt for writing\n");

    inputFile = fopen("txin.txt", "w");

    if (!inputFile) sysErrFatal("couldn't open file inputs.txt for writing\n");

    outputFile = fopen("txout.txt", "w");

    if (!outputFile) sysErrFatal("couldn't open file outputs.txt for writing\n");

    FILE *sqlFile = fopen("blockchain.sql", "w");

    if (!sqlFile) sysErrFatal("couldn't open file blockChain.sql for writing\n");

    fprintf(
      sqlFile,
      "PRAGMA journal_mode=MEMORY;\n"
      "PRAGMA synchronous=0;\n"
      "CREATE TABLE IF NOT EXISTS blocks(\n"
      "    block_id BIGINT NOT NULL PRIMARY KEY,\n"
      "    block_hash TEXT NOT NULL,\n"
      "    time BIGINT NOT NULL\n"
      ");\n"
      "\n"
      "CREATE TABLE IF NOT EXISTS tx(\n"
      "    tx_id BIGINT NOT NULL PRIMARY KEY,\n"
      "    tx_hash TEXT NOT NULL,\n"
      "    block_id BIGINT NOT NULL,\n"
      "    FOREIGN KEY (block_id) REFERENCES blocks (block_id)\n"
      ");\n"
      "\n"
      "CREATE TABLE IF NOT EXISTS txout(\n"
      "    txout_id BIGINT NOT NULL PRIMARY KEY,\n"
      "    address CHAR(40),\n"
      "    txout_value BIGINT NOT NULL,\n"
      "    tx_id BIGINT NOT NULL,\n"
      "    txout_pos INT NOT NULL,\n"
      "    FOREIGN KEY (tx_id) REFERENCES tx (tx_id)\n"
      ");\n"
      "\n"
      "CREATE TABLE IF NOT EXISTS txin(\n"
      "    txin_id BIGINT NOT NULL PRIMARY KEY,\n"
      "    txout_id BIGINT NOT NULL,\n"
      "    tx_id BIGINT NOT NULL,\n"
      "    txin_pos INT NOT NULL,\n"
      "    FOREIGN KEY (tx_id) REFERENCES tx (tx_id)\n"
      ");\n"
      "CREATE INDEX IF NOT EXISTS x_txin_txout ON txin (txout_id);\n"
      "CREATE INDEX IF NOT EXISTS x_txout_address ON txout (address);\n"
      "CREATE INDEX IF NOT EXISTS x_txin_txid ON txin (tx_id);\n"
      "CREATE INDEX IF NOT EXISTS x_txout_txid ON txout (tx_id);\n"
      "CREATE INDEX IF NOT EXISTS x_txout_value ON txout (txout_value);\n"
      "CREATE VIEW IF NOT EXISTS tx_full AS SELECT blocks.time, tx.tx_hash, tx.tx_id, txout.address, txout.txout_value FROM txout LEFT JOIN tx ON (tx.tx_id = txout.tx_id) LEFT JOIN blocks ON (tx.block_id = blocks.block_id);\n"
      "\n"
      );
    fclose(sqlFile);

    FILE *bashFile = fopen("blockchain.sh", "w");

    if (!bashFile) sysErrFatal("Couldn't open file blockchain.sh for writing!\n");


    fprintf(
      bashFile,
      "\n"
      "#!/bin/bash\n"
      "\n"
      "echo 'Recreating DB blockchain...'\n"
      "mkdir ../blockchain\n"
      "sqlite3 ../blockchain/blockchain.sqlite < blockchain.sql\n"
      "rm -f blockchain.sql\n"
      "echo done.\n"
      "echo\n"
      "rm -f blockchain.sql\n"
      "\n"
      "for i in blocks txin txout tx\n"
      "do\n"
      "    echo \"Importing table ${i}...\"\n"
      "    echo \".import $i.txt $i\" | sqlite3 ../blockchain/blockchain.sqlite\n"
      "    echo done.\n"
      "    rm -f $i.txt\n"
      "    echo\n"
      "done\n"
      "rm -f blockchain.sh\n"
      "\n"
      );
    fclose(bashFile);

    return 0;
  }
Exemplo n.º 2
0
    virtual int init(
        int argc,
        const char *argv[]
    )
    {
        txID = 0;
        blkID = 0;
        inputID = 0;
        outputID = 0;

        static uint64_t sz = 32 * 1000 * 1000;
        outputMap.setEmptyKey(empty);
        outputMap.resize(sz);

        optparse::Values &values = parser.parse_args(argc, argv);
        cutoffBlock = values.get("atBlock");

        info("dumping the blockchain ...");

        txFile = fopen("transactions.txt", "w");
        if(!txFile) sysErrFatal("couldn't open file txs.txt for writing\n");

        blockFile = fopen("blocks.txt", "w");
        if(!blockFile) sysErrFatal("couldn't open file blocks.txt for writing\n");

        inputFile = fopen("inputs.txt", "w");
        if(!inputFile) sysErrFatal("couldn't open file inputs.txt for writing\n");

        outputFile = fopen("outputs.txt", "w");
        if(!outputFile) sysErrFatal("couldn't open file outputs.txt for writing\n");

        FILE *sqlFile = fopen("blockChain.sql", "w");
        if(!sqlFile) sysErrFatal("couldn't open file blockChain.sql for writing\n");

        fprintf(
            sqlFile,
            "\n"
            "DROP DATABASE IF EXISTS blockChain;\n"
            "CREATE DATABASE blockChain;\n"
            "USE blockChain;\n"
            "\n"
            "DROP TABLE IF EXISTS transactions;\n"
            "DROP TABLE IF EXISTS outputs;\n"
            "DROP TABLE IF EXISTS inputs;\n"
            "DROP TABLE IF EXISTS blocks;\n"
            "\n"
            "CREATE TABLE blocks(\n"
            "    id BIGINT PRIMARY KEY,\n"
            "    hash BINARY(32),\n"
            "    time BIGINT\n"
            ");\n"
            "\n"
            "CREATE TABLE transactions(\n"
            "    id BIGINT PRIMARY KEY,\n"
            "    hash BINARY(32),\n"
            "    blockID BIGINT\n"
            ");\n"
            "\n"
            "CREATE TABLE outputs(\n"
            "    id BIGINT PRIMARY KEY,\n"
            "    dstAddress CHAR(36),\n"
            "    value BIGINT,\n"
            "    txID BIGINT,\n"
            "    offset INT\n"
            ");\n"
            "\n"
            "CREATE TABLE inputs(\n"
            "    id BIGINT PRIMARY KEY,\n"
            "    outputID BIGINT,\n"
            "    txID BIGINT,\n"
            "    offset INT\n"
            ");\n"
            "\n"
        );
        fclose(sqlFile);

        FILE *bashFile = fopen("blockChain.bash", "w");
        if(!bashFile) sysErrFatal("couldn't open file blockChain.bash for writing\n");

        fprintf(
            bashFile,
            "\n"
            "#!/bin/bash\n"
            "\n"
            "echo\n"
            "\n"
            "echo 'wiping/re-creating DB blockChain ...'\n"
            "time mysql -u root -p -hlocalhost --password='' < blockChain.sql\n"
            "echo done\n"
            "echo\n"
            "\n"
            "for i in blocks inputs outputs transactions\n"
            "do\n"
            "    echo Importing table $i ...\n"
            "    time mysqlimport -u root -p -hlocalhost --password='' --lock-tables --use-threads=3 --local blockChain $i.txt\n"
            "    echo done\n"
            "    echo\n"
            "done\n"
            "\n"
        );
        fclose(bashFile);

        return 0;
    }