Пример #1
0
int main(int argc, char* argv[])
{
    try
    {
        NetworkSelector networkSelector;

        if (argc < 3)
        {
            cerr << "# Usage: " << argv[0] << " <network> <host> [port] [bloom filter elements = 10]" << endl
                 << "# Supported networks: " << stdutils::delimited_list(networkSelector.getNetworkNames(), ", ") << endl;
            return -1;
        }

        cout << endl << "Initializing logger to file netsync.log..." << endl;
        INIT_LOGGER("netsync.log");

        CoinParams coinParams = networkSelector.getCoinParams(argv[1]);
        string host = argv[2];
        string port = (argc > 3) ? argv[3] : coinParams.default_port();
        unsigned int filterElements = (argc > 4) ? strtoul(argv[4], NULL, 0) : 10;

        cout << endl << "Connecting to " << coinParams.network_name() << " peer" << endl
             << "-------------------------------------------" << endl
             << "  host:             " << host << endl
             << "  port:             " << port << endl
             << "  magic bytes:      " << hex << coinParams.magic_bytes() << endl
             << "  protocol version: " << dec << coinParams.protocol_version() << endl
             << endl;

        unsigned int blockTxIndex = 0;

        Network::NetworkSync networkSync(coinParams);
        networkSync.loadHeaders("blocktree.dat", false, [&](const CoinQBlockTreeMem& blocktree) {
            cout << "Best height: " << blocktree.getBestHeight() << " Total work: " << blocktree.getTotalWork().getDec() << endl;
            return !g_bShutdown;
        });

        if (g_bShutdown)
        {
            cout << "Interrupted." << endl;
            return 0;
        }

        networkSync.subscribeStarted([&]() {
            cout << endl << "NetworkSync started." << endl;
        });

        networkSync.subscribeStopped([&]() {
            cout << endl << "NetworkSync stopped." << endl;
        });

        networkSync.subscribeOpen([&]() {
            cout << endl << "NetworkSync open." << endl;
        });

        networkSync.subscribeClose([&]() {
            cout << endl << "NetworkSync closed." << endl;
        });

        networkSync.subscribeTimeout([&]() {
            cout << endl << "NetworkSync timeout." << endl;
        });

        networkSync.subscribeConnectionError([&](const string& error, int /*code*/) {
            cout << endl << "NetworkSync connection error: " << error << endl;
        });

        networkSync.subscribeProtocolError([&](const string& error, int /*code*/) {
            cout << endl << "NetworkSync protocol error: " << error << endl;
        });

        networkSync.subscribeBlockTreeError([&](const string& error, int /*code*/) {
            cout << endl << "NetworkSync block tree error: " << error << endl;
        });

        networkSync.subscribeSynchingHeaders([&]() {
            cout << endl << "NetworkSync fetching headers." << endl;
        });

        networkSync.subscribeHeadersSynched([&]() {
            cout << endl << "NetworkSync headers synched." << endl;
            hashvector_t hashes;
            networkSync.syncBlocks(hashes, time(NULL) - 10*60*60); // Start 10 hours earlier
        });

        networkSync.subscribeSynchingBlocks([&]() {
            cout << endl << "NetworkSync fetching blocks." << endl;
        });

        networkSync.subscribeBlocksSynched([&]() {
            cout << endl << "NetworkSync blocks synched." << endl;

            cout << endl << "Fetching mempool..." << endl;
            networkSync.getMempool();
        });

        networkSync.subscribeStatus([&](const string& status) {
            cout << endl << "NetworkSync status: " << status << endl;
        });

        networkSync.subscribeNewTx([&](const Transaction& tx) {
            cout << endl << "NEW TX: " << tx.getHashLittleEndian().getHex() << endl;
        });

        networkSync.subscribeMerkleTx([&](const ChainMerkleBlock& merkleBlock, const Transaction& tx, unsigned int txIndex, unsigned int txTotal)
        {
            cout << "  tx (" << txIndex << "/" << (txTotal - 1) << "): " << tx.getHashLittleEndian().getHex() << endl;
        });

        networkSync.subscribeBlock([&](const ChainBlock& block) {
            cout << endl << "NEW BLOCK: " << block.blockHeader.getHashLittleEndian().getHex() << " height: " << block.height << endl;
        });

        networkSync.subscribeMerkleBlock([&](const ChainMerkleBlock& merkleblock) {
            cout << endl << "NEW MERKLE BLOCK" << endl
                         << "  hash: " << merkleblock.blockHeader.getHashLittleEndian().getHex() << endl
                         << "  height: " << merkleblock.height << endl;

            try
            {
                PartialMerkleTree tree(merkleblock.nTxs, merkleblock.hashes, merkleblock.flags, merkleblock.merkleRoot());
                std::vector<uchar_vector> txhashes = tree.getTxHashesLittleEndianVector();
                unsigned int i = 0;
                cout << "should contain txs:" << endl;
                for (auto& txhash: txhashes) { cout << "  tx " << i++ << ": " << uchar_vector(txhash).getHex() << endl; }
            }
            catch (const exception& e)
            {
                cout << "Error constructing partial merkle tree: " << e.what() << endl;
            }

            cout << "--------------------" << endl;
            blockTxIndex = 0;
        });

        networkSync.subscribeAddBestChain([&](const ChainHeader& header) {
            cout << endl << "NetworkSync added to best chain: " << header.getHashLittleEndian().getHex() << " height: " << header.height << endl;
        });

        networkSync.subscribeRemoveBestChain([&](const ChainHeader& header) {
            cout << endl << "NetworkSync removed from best chain: " << header.getHashLittleEndian().getHex() << " height: " << header.height << endl;
        });

        networkSync.subscribeBlockTreeChanged([&]() {
            cout << endl << "NetworkSync block tree changed." << endl;
        });

        // Set the bloom filter
        cout << endl << "Bloom filter settings" << endl
                     << "---------------------" << endl
                     << "  elements:            " << filterElements << endl
                     << "  false positive rate: 0.001" << endl
                     << "  nTweak:              0" << endl
                     << "  nFlags:              0" << endl;

        BloomFilter filter(filterElements, 0.001, 0, 0);
        for (unsigned int i = 0; i < filterElements; i++) { filter.insert(random_bytes(32)); }
        networkSync.setBloomFilter(filter);

        cout << endl << "Registering SIGINT and SIGTERM signal handlers.." << endl;
        signal(SIGINT, &finish);
        signal(SIGTERM, &finish);

        LOGGER(info) << endl << endl << endl << endl << endl << endl;
        cout << endl << "Starting sync..." << endl;
        LOGGER(info) << "Starting..." << endl;
        networkSync.start(host, port);
        while (!g_bShutdown) {

            boost::this_thread::sleep(boost::posix_time::microseconds(200));
            //usleep(200);

        }

        cout << "Stopping..." << endl;
        LOGGER(info) << "Stopping..." << endl;
        networkSync.stop();

        cout << "Stopped." << endl;
        LOGGER(info) << "Stopped." << endl;
    }
    catch (const exception& e)
    {
        cerr << "Unexpected termination. Error: " << e.what() << endl;
        LOGGER(error) << "Unexpected termination. Error: " << e.what() << endl;
        return -2;
    }

    return 0;
}
Пример #2
0
int main(int argc, char* argv[])
{
    try
    {
        NetworkSelector networkSelector;

        if (argc < 3)
        {
            cerr << "# Usage: " << argv[0] << " <network> <host> [port]" << endl
                 << "# Supported networks: " << stdutils::delimited_list(networkSelector.getNetworkNames(), ", ") << endl;
            return -1;
        }

        CoinParams coinParams = networkSelector.getCoinParams(argv[1]);
        string host = argv[2];
        string port = (argc > 3) ? argv[3] : coinParams.default_port();

        CoinQ::io_service_t io_service;
        CoinQ::io_service_t::work work(io_service);
        boost::thread thread(boost::bind(&CoinQ::io_service_t::run, &io_service));


        cout << endl << "Connecting to " << coinParams.network_name() << " peer" << endl
             << "-------------------------------------------" << endl
             << "  host:             " << host << endl
             << "  port:             " << port << endl
             << "  magic bytes:      " << hex << coinParams.magic_bytes() << endl
             << "  protocol version: " << dec << coinParams.protocol_version() << endl
             << endl;

        Peer peer(io_service);
        peer.set(host, port, coinParams.magic_bytes(), coinParams.protocol_version(), "peer v1.0", 0, true);

        peer.subscribeStart([&](Peer& peer) { cout << "Peer " << peer.name() << " started." << endl; });
        peer.subscribeStop([&](Peer& peer) { cout << "Peer " << peer.name() << " stopped." << endl; });
        peer.subscribeOpen(&onOpen);
        peer.subscribeClose(&onClose);
        peer.subscribeConnectionError(&onConnectionError);

        peer.subscribeTimeout([&](Peer& peer) { cout << "Peer " << peer.name() << " timed out." << endl; });

        peer.subscribeInv(&onInv);
        peer.subscribeHeaders(&onHeaders);
        peer.subscribeTx(&onTx);
        peer.subscribeBlock(&onBlock);
        peer.subscribeMerkleBlock(&onMerkleBlock);
        peer.subscribeProtocolError(&onProtocolError);

        INIT_LOGGER("peer.log");
    
        signal(SIGINT, &finish);
        signal(SIGTERM, &finish);

        peer.start();
        while (!g_bShutdown) { usleep(200); }
        peer.stop();
    }
    catch (const exception& e)
    {
        cerr << "Error: " << e.what() << endl;
        return -2;
    }

    return 0;
}
Пример #3
0
int main(int argc, char* argv[])
{
    try
    {
        NetworkSelector networkSelector;

        if (argc < 3)
        {
            cerr << "# Usage: " << argv[0] << " <network> <host> [start hash] [start height] [port]" << endl
                 << "# Supported networks: " << stdutils::delimited_list(networkSelector.getNetworkNames(), ", ") << endl;
            return -1;
        }

        CoinParams coinParams = networkSelector.getCoinParams(argv[1]);
        string host = argv[2];

        vector<uchar_vector> locatorHashes;
        if (argc > 3) { locatorHashes.push_back(uchar_vector(argv[3])); }

        int startHeight = (argc > 4) ? strtoll(argv[4], NULL, 0) : 0;

        string port = (argc > 5) ? argv[5] : coinParams.default_port();

        Network::BlockchainDownload download(coinParams);

        cout << endl << "Connecting to " << coinParams.network_name() << " peer" << endl
             << "-------------------------------------------" << endl
             << "  host:             " << host << endl
             << "  port:             " << port << endl
             << "  magic bytes:      " << hex << coinParams.magic_bytes() << endl
             << "  protocol version: " << dec << coinParams.protocol_version() << endl
             << endl;

        download.subscribeStarted([&]()         { cout << "Blockchain download started." << endl; });
        download.subscribeStopped([&]()         { cout << "Blockchain download stopped." << endl; });
        download.subscribeOpen([&]()            { cout << "Peer connection opened." << endl; });
        download.subscribeClose([&]()           { cout << "Peer connection closed." << endl; });
        download.subscribeTimeout([&]()         { cout << "Peer timed out." << endl; });
        download.subscribeConnectionError([&](const string& error, int /*code*/) { cout << "Connection error: " << error << endl; });

        download.subscribeBlock([&](const Coin::CoinBlock& block)    { cout << "Block - hash: " << block.hash().getHex() << " height: " << download.getBestHeight() + startHeight + 1 << endl; });
        download.subscribeProtocolError([&](const string& error, int /*code*/)   { cout << "Protocol error:" << error << endl; });

        INIT_LOGGER("blockchain.log");
    
        signal(SIGINT, &finish);
        signal(SIGTERM, &finish);

        download.start(host, port, locatorHashes);

        while (!g_bShutdown) { usleep(200); }
        download.stop();
    }
    catch (const exception& e)
    {
        cerr << "Error: " << e.what() << endl;
        return -2;
    }

    return 0;
}