Esempio n. 1
0
    virtual int init(
        int argc,
        const char *argv[]
    )
    {
        optparse::Values &values = parser.parse_args(argc, argv);

        auto args = parser.args();
        for(size_t i=1; i<args.size(); ++i) {
            loadKeyList(rootHashes, args[i].c_str());
        }

        if(0==rootHashes.size()) {
            const char *addr = "1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp";
            warning("no addresses specified, using satoshi's dice address %s", addr);
            loadKeyList(rootHashes, addr);
        }

        addrMap.setEmptyKey(gEmptyKey);
        addrMap.resize(15 * 1000 * 1000);
        allAddrs.reserve(15 * 1000 * 1000);
        info("Building address equivalence graph ...");
        startTime = usecs();

        return 0;
    }
Esempio n. 2
0
    virtual int init(
        int        argc,
        const char *argv[]
    ) {

        nbDumped = 0;
        isDone = false;

        optparse::Values &values = parser.parse_args(argc, argv);

        auto args = parser.args();
        for(size_t i=1; i<args.size(); ++i) {
            loadHash256List(rootHashes, args[i].c_str());
        }

        if(0<rootHashes.size()) {
            info("dumping %d transactions\n", (int)rootHashes.size());
        } else {
            const char *defaultTX = "a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d"; // Expensive pizza
            warning("no TX hashes specified, using the famous 10K pizza TX");
            loadHash256List(rootHashes, defaultTX);
        }

        static uint8_t empty[kSHA256ByteSize] = { 0x42 };
        txMap.setEmptyKey(empty);

        for(auto const &txHash : rootHashes) {
            txMap[txHash.v] = 1;
        }

        return 0;
    }
Esempio n. 3
0
    virtual int init(
        int argc,
        const char *argv[]
    )
    {
        txID = 0;
        blkID = 0;
        active = 0;

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

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

        blockFile = fopen("blocks.csv", "w");
        if(!blockFile) sysErrFatal("couldn't open file blocks.csv for writing\n");
        fprintf(blockFile, "ID,Hash,Version,Timestamp,Nonce,Difficulty,Merkle,NumTransactions,OutputValue,FeesValue,Size\n");

        txFile = fopen("transactions.csv", "w");
        if(!txFile) sysErrFatal("couldn't open file txs.csv for writing\n");
        fprintf(txFile, "ID,Hash,Version,BlockId,NumInputs,NumOutputs,OutputValue,FeesValue,LockTime,Size\n");

        outputFile = fopen("outputs.csv", "w");
        if(!outputFile) sysErrFatal("couldn't open file outputs.csv for writing\n");
        fprintf(outputFile, "TransactionId,Index,Value,Script,ReceivingAddress,InputTxHash,InputTxIndex\n");

        inputFile = fopen("inputs.csv", "w");
        if(!inputFile) sysErrFatal("couldn't open file inputs.csv for writing\n");
        fprintf(inputFile, "TransactionId,Index,Script,OutputTxHash,OutputTxIndex\n");

        return 0;
    }
Esempio n. 4
0
    virtual int init(
        int argc,
        const char *argv[]
    )
    {
        optparse::Values &values = parser.parse_args(argc, argv);

        auto args = parser.args();
        for(size_t i=1; i<args.size(); ++i) {
            loadKeyList(rootHashes, args[i].c_str());
        }

        if(0==rootHashes.size()) {
            const char *addr = getInterestingAddr();
            loadKeyList(rootHashes, addr);
        }

        addrMap.setEmptyKey(gEmptyKey);
        addrMap.resize(15 * 1000 * 1000);
        allAddrs.reserve(15 * 1000 * 1000);
        info("Building address equivalence graph ...");
        startTime = usecs();

        return 0;
    }
Esempio n. 5
0
static
bam2sam::Settings fromCommandLine(optparse::OptionParser& parser,
                                    int argc, char* argv[])
{
    bam2sam::Settings settings;

    const optparse::Values options = parser.parse_args(argc, argv);

    // input
    const std::vector<std::string> positionalArgs = parser.args();
    const size_t numPositionalArgs = positionalArgs.size();
    if (numPositionalArgs == 0)
        settings.inputFilename_ = "-"; // stdin
    else if (numPositionalArgs == 1)
        settings.inputFilename_ = parser.args().front();
    else {
        assert(numPositionalArgs > 1);
        settings.errors_.push_back("bam2sam does not support more than one input file per run");
    }

    // header options
    if (options.is_set("no_header"))
        settings.noHeader_ = options.get("no_header");
    if (options.is_set("header_only"))
        settings.printHeaderOnly_ = options.get("header_only");

    if (settings.noHeader_ && settings.printHeaderOnly_)
        settings.errors_.push_back("conflicting arguments requested: --no-header and --header-only");

    return settings;
}
Esempio n. 6
0
    virtual int init(
        int argc,
        const char *argv[]
    ) {
        optparse::Values &values = parser.parse_args(argc, argv);
        bool longHelp = values.get("long");
        longHelp = longHelp || (
            argv[1]         &&
            'm'==argv[1][0] &&
            'a'==argv[1][1] &&
            'n'==argv[1][2]
        );
        longHelp = longHelp || (
            argv[1]         &&
            'd'==argv[1][0] &&
            'o'==argv[1][1] &&
            'c'==argv[1][2]
        );

        auto args = parser.args();
        if(1<args.size()) {
            for(size_t i=1; i<args.size(); ++i) { // O(N^2), but short lists
                auto arg = args[i];
                Callback::showHelpFor(
                    arg.c_str(),
                    longHelp
                );
            }
        } else {
            printf("\n");
            printf("General Usage: parser <command> <options> <command arguments>\n");
            printf("\n");
            printf("    Where <command> can be any of:\n");
            Callback::find("", true);
            printf("\n");
            printf("    NOTE: use \"parser help <command>\" or \"parser <command> --help\" to get detailed\n");
            printf("          help for a specific command.\n");
            printf("\n");
            printf("    NOTE: <command> may have multiple aliases and can also be abbreviated. For\n");
            printf("          example, \"parser tx\", \"parser tr\", and \"parser transactions\" are equivalent.\n");
            printf("\n");
            printf("    NOTE: whenever specifying a list of things (e.g. a list of addresses), you can\n");
            printf("          instead enter \"file:list.txt\" and the list will be read from the file.\n");
            printf("\n");
            printf("    NOTE: whenever specifying a list file, you can use \"file:-\" and blockparser\n");
            printf("          will read the list directly from stdin.\n");
            printf("\n");
            printf("\n");

            if(longHelp) {
                printf("Every available command and its specific usage follows:\n");
                printf("\n");
                Callback::showAllHelps(false);
            }
        }
        return 0;
    }
Esempio n. 7
0
    virtual int init(
        int argc,
        const char *argv[]
    )
    {
        offset = 0;
        curBlock = 0;
        lastBlock = 0;
        firstBlock = 0;

        addrMap.setEmptyKey(emptyKey);
        addrMap.resize(15 * 1000 * 1000);
        allAddrs.reserve(15 * 1000 * 1000);

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

        auto args = parser.args();
        for(size_t i=1; i<args.size(); ++i) {
            loadKeyList(restricts, args[i].c_str());
        }

        if(0<=cutoffBlock) {
            info("only taking into account transactions before block %" PRIu64 "\n", cutoffBlock);
        }

        if(0!=restricts.size()) {

            info(
                "restricting output to %" PRIu64 " addresses ...\n",
                (uint64_t)restricts.size()
            );

            auto e = restricts.end();
            auto i = restricts.begin();
            restrictMap.setEmptyKey(emptyKey);
            while(e!=i) {
                const uint160_t &h = *(i++);
                restrictMap[h.v] = 1;
            }
        } else {
            if(detailed) {
                warning("asking for --detailed for *all* addresses in the blockchain will be *very* slow");
                warning("as a matter of fact, it likely won't ever finish unless you have *lots* of RAM");
            }
        }

        info("analyzing blockchain ...");
        return 0;
    }
Esempio n. 8
0
    virtual int init(
        int argc,
        const char *argv[]
    )
    {
        optparse::Values &values = parser.parse_args(argc, argv);
        //fullDump = values.get("full");

        info("parsing high level peercoin stats");
        POScount = 0;
        POWcount = 0;
        totalFeeDestroyed = 0;
        totalMined = 0;
        totalStakeEarned = 0;
        totalStaked = 0;
        totalTrans = 0;
        totalSent = 0;
        totalReceived = 0;
        return 0;
    }
Esempio n. 9
0
    virtual int init(
        int argc,
        const char *argv[]
    )
    {
        nbDumped = 0;

        optparse::Values &values = parser.parse_args(argc, argv);

        auto args = parser.args();
        for(size_t i=1; i<args.size(); ++i) {
            loadHash256List(rootHashes, args[i].c_str());
        }

        if(0<rootHashes.size()) {
            info("dumping %d transactions\n", (int)rootHashes.size());
        } else {
#if defined(PEERCOIN)
            const char *defaultTX = "19093c85669bf82c9baa70eb437e2f319409f40b54b2c5ebc4dd334ab610fbe6";
            warning("no TX hashes specified, using a random proof of stake peercoin transaction");
#else
            const char *defaultTX = "a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d"; // Expensive pizza
            warning("no TX hashes specified, using the infamous 10K pizza TX");
#endif
            loadHash256List(rootHashes, defaultTX);
        }

        static uint8_t empty[kSHA256ByteSize] = { 0x42 };
        txMap.setEmptyKey(empty);

        for(auto it = rootHashes.begin(); it != rootHashes.end(); it++) {
            auto const &txHash = *it;
            txMap[txHash.v] = 1;
        }

        //for(auto const &txHash : rootHashes) {
        //    txMap[txHash.v] = 1;
        //}

        return 0;
    }
Esempio n. 10
0
static
pbindexdump::Settings fromCommandLine(optparse::OptionParser& parser,
                                      int argc,
                                      char* argv[])
{
    const optparse::Values options = parser.parse_args(argc, argv);
    pbindexdump::Settings settings;

    // input
    const vector<string> positionalArgs = parser.args();
    const size_t numPositionalArgs = positionalArgs.size();
    if (numPositionalArgs == 0)
        settings.inputPbiFilename_ = "-"; // stdin
    else if (numPositionalArgs == 1)
        settings.inputPbiFilename_ = parser.args().front();
    else {
        assert(numPositionalArgs > 1);
        settings.errors_.push_back("pbindexdump does not support more than one input file per run");
    }

    // output format
    if (options.is_set("format"))
        settings.format_ = options["format"];

    // JSON options
    if (settings.format_ == "json") {
        if (options.is_set("json_indent_level"))
            settings.jsonIndentLevel_ = options.get("json_indent_level");
        if (options.is_set("json_raw"))
            settings.jsonRaw_ = options.get("json_raw");
    } else {
        if (options.is_set("json_indent_level") ||
            options.is_set("json_raw"))
        {
            settings.errors_.push_back("JSON formatting options not valid on non-JSON output");
        }
    }

    return settings;
}
Esempio n. 11
0
    virtual int init(
        int argc,
        const char *argv[]
    )
    {
        threshold = 1e-20;

        optparse::Values &values = parser.parse_args(argc, argv);

        auto args = parser.args();
        for(size_t i=1; i<args.size(); ++i) {
            loadHash256List(rootHashes, args[i].c_str());
        }

        if(0<rootHashes.size()) {
            info("computing taint from %d source transactions\n", (int)rootHashes.size());
        } else {
            warning("no TX hashes specified, using the infamous 10K pizza TX");

            //const char *defaultTX = "34b84108a142ad7b6c36f0f3549a3e83dcdbb60e0ba0df96cd48f852da0b1acb"; // Linode slush hack
            const char *defaultTX = "a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d"; // Expensive pizza
            loadHash256List(rootHashes, defaultTX);
        }

        static uint8_t empty[kSHA256ByteSize] = { 0x42 };
        static unsigned int sz = 15 * 1000 * 1000;
        srcTxMap.setEmptyKey(empty);
        taintMap.setEmptyKey(empty);
        taintMap.resize(sz);

        auto i = rootHashes.begin();
        auto e = rootHashes.end();
        while(e!=i) {
            const uint256_t &txHash = *(i++);
            taintMap[txHash.v] = 1.0;
            srcTxMap[txHash.v] = 1;
        }

        return 0;
    }
Esempio n. 12
0
    virtual int init(
        int  argc,
        const char *argv[]
    )
    {
        sum = 0;
        adds = 0;
        subs = 0;
        nbTX = 0;

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

        auto args = parser.args();
        for(size_t i=1; i<args.size(); ++i) {
            loadKeyList(rootHashes, args[i].c_str());
        }

        if(0==rootHashes.size()) {
            #if defined(LITECOIN)
                const char *addr = "LKvTVnkK2rAkJXfgPdkaDRgvEGvazxWS9o";
                warning("no addresses specified, using popular address %s", addr);
            #else
                const char *addr = "1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp";
                warning("no addresses specified, using satoshi's dice address %s", addr);
            #endif
            loadKeyList(rootHashes, addr);
        }

        auto e = rootHashes.end();
        auto i = rootHashes.begin();
        addrMap.setEmptyKey(emptyKey);
        while(e!=i) {
            const uint160_t &h = *(i++);
            addrMap[h.v] = 1;
        }
        return 0;
    }
Esempio n. 13
0
static
pbindex::Settings fromCommandLine(optparse::OptionParser& parser,
                                  int argc, char* argv[])
{
    const optparse::Values options = parser.parse_args(argc, argv);
    (void)options;

    pbindex::Settings settings;

    // get input filename
    const vector<string> positionalArgs = parser.args();
    const size_t numPositionalArgs = positionalArgs.size();
    if (numPositionalArgs == 0)
        settings.errors_.push_back("pbindex requires an input BAM filename");
    else if (numPositionalArgs == 1)
        settings.inputBamFilename_ = parser.args().front();
    else {
        assert(numPositionalArgs > 1);
        settings.errors_.push_back("pbindex does not support more than one input file per run");
    }

    return settings;
}
Esempio n. 14
0
Settings Settings::FromCommandLine(optparse::OptionParser& parser,
                                   int argc,
                                   char *argv[],
                                   bool forcePulseMode)
{
    Settings settings;

    // general program info
    settings.program = parser.prog();
    settings.description = parser.description();
    settings.version = parser.version();
    for (int i = 1; i < argc; ++i) {
        settings.args.append(argv[i]);
        settings.args.append(" ");
    }

    const optparse::Values options = parser.parse_args(argc, argv);

    // mode
    settings.ignoreQV = options.is_set(Settings::Option::ignoreQV_);

    const bool isBaseMode = 
        options.is_set(Settings::Option::baseMode_) ? options.get(Settings::Option::baseMode_) : false;

    const bool isPulseMode = 
        options.is_set(Settings::Option::pulseMode_) ? options.get(Settings::Option::pulseMode_) : false;

    int modeCount = 0;
    if (isBaseMode)  modeCount++;
    if (isPulseMode) modeCount++;

    if (modeCount == 0)
        settings.mode = Settings::BaseMode;
    else if (modeCount == 1) 
        if (isBaseMode)  
            settings.mode = Settings::BaseMode;
        else 
            settings.mode = Settings::PulseMode;
    else
        settings.errors_.push_back("Unknown modes selected.");

    if (forcePulseMode) settings.mode = Settings::PulseMode;

    // BaseMap
    if (not options[Settings::Option::baseMap_].empty()) {
        settings.baseMap = options[Settings::Option::baseMap_];
        std::transform(settings.baseMap.begin(), settings.baseMap.end(), settings.baseMap.begin(), ::toupper);
        cout << settings.baseMap << endl;
        std::string _baseMap = settings.baseMap;
        std::sort(_baseMap.begin(), _baseMap.end());
        if (_baseMap != "ACGT") { settings.errors_.push_back("Bad basemap."); }
    }

    // input
    settings.inputBamFilenames = parser.args();
    if (settings.inputBamFilenames.size() == 1) {
        settings.polymeraseBamFilename = settings.inputBamFilenames[0];
        if (settings.polymeraseBamFilename.find("polymerase.bam") == std::string::npos)
            settings.errors_.push_back("missing input *.polymerase.bam.");
    } else if (settings.inputBamFilenames.size() == 2) {
        settings.subreadsBamFilename = settings.inputBamFilenames[0];
        settings.scrapsBamFilename   = settings.inputBamFilenames[1];
        if (settings.subreadsBamFilename.find("subreads.bam") == std::string::npos)
            settings.errors_.push_back("missing input *.subreads.bam.");
        if (settings.scrapsBamFilename.find("scraps.bam") == std::string::npos)
            settings.errors_.push_back("missing input *.scraps.bam.");
    } else {
        settings.errors_.push_back("missing input (polymerase.bam or subreads+scraps.bam.");
    }

    // output 
    settings.outputBaxPrefix = options[Settings::Option::output_];
    if (settings.outputBaxPrefix.empty()) { // if output prefix not set.
        if (not settings.subreadsBamFilename.empty()) {
            settings.outputBaxPrefix = internal::GetMovienameFromFilename(settings.subreadsBamFilename);
        } else if (not settings.polymeraseBamFilename.empty()) {
            settings.outputBaxPrefix = internal::GetMovienameFromFilename(settings.polymeraseBamFilename);
        }
    }

    if (settings.mode == Settings::BaseMode) 
        settings.outputBaxFilename = settings.outputBaxPrefix + ".bax.h5";
    else if (settings.mode == Settings::PulseMode)
        settings.outputBaxFilename = settings.outputBaxPrefix + ".plx.h5";
    settings.outputRgnFilename = settings.outputBaxPrefix + ".rgn.h5";

    // movie
    settings.movieName = internal::GetMovienameFromFilename(settings.outputBaxPrefix);

    if (options.is_set(Settings::Option::metadata_)) {
        // metadata.xml will be placed at upper directory of bax.h5
        settings.outputMetadataFilename = internal::DirectoryPath(settings.outputBaxPrefix) + "/../" + 
                                          settings.movieName + ".metadata.xml";
        settings.outputAnalysisDirname = internal::ParentDirectoryName(internal::DirectoryPath(settings.outputBaxPrefix));
    }

#ifdef DEBUG_SETTINGS
    string modeString = "Unknown";
    if (settings.mode == Settings::BaseMode)
        modeString = "base";
    else if (settings.mode == Settings::PulseMode)
        modeString = "pulse";

    cerr << "CommandLine: " << settings.program << " " << settings.args << endl
         << "Description: " << settings.description << endl
         << "Version    : " << settings.version << endl
         << "Mode       : " << modeString << endl
         << "BaseMap    : " << settings.baseMap << endl
         << "Movie name : " << settings.movieName << endl
         << "Input files: " << endl;
    if (not settings.subreadsBamFilename.empty())
         cerr << " subreads  : " << settings.subreadsBamFilename << endl;
    if (not settings.scrapsBamFilename.empty())
         cerr << " scraps    : " << settings.scrapsBamFilename << endl;
    if (not settings.polymeraseBamFilename.empty())
         cerr << " polymerase: " << settings.polymeraseBamFilename << endl;
    cerr << "Output h5  : " << settings.outputBaxFilename << endl
         << "Output xml : " << settings.outputMetadataFilename << endl;

#endif

    return settings;
}
Esempio n. 15
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;
  }
Esempio n. 16
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;
    }