From 26ba9efff5d443b01b999b966d7ec48ba2080318 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Mon, 16 Jul 2018 08:11:25 +0800 Subject: [PATCH] MicroCore reorganized and mempool reading upgrades --- .../source/corvusoft/restbed/request.cpp | 24 ++--- html/js/config.js | 2 +- src/CurrentBlockchainStatus.cpp | 78 ++++++---------- src/CurrentBlockchainStatus.h | 5 +- src/MicroCore.cpp | 93 ++++++++----------- src/MicroCore.h | 69 +++++++++++--- src/rpccalls.cpp | 35 +------ src/rpccalls.h | 3 - tests/mysql_tests.cpp | 2 +- 9 files changed, 142 insertions(+), 169 deletions(-) diff --git a/ext/restbed/source/corvusoft/restbed/request.cpp b/ext/restbed/source/corvusoft/restbed/request.cpp index a8f03da..334f21f 100644 --- a/ext/restbed/source/corvusoft/restbed/request.cpp +++ b/ext/restbed/source/corvusoft/restbed/request.cpp @@ -139,11 +139,11 @@ namespace restbed { header = stof( get_header( name ) ); } - catch ( const out_of_range ) + catch ( const out_of_range& ) { header = default_value; } - catch ( const invalid_argument ) + catch ( const invalid_argument& ) { header = default_value; } @@ -159,11 +159,11 @@ namespace restbed { header = stod( get_header( name ) ); } - catch ( const out_of_range ) + catch ( const out_of_range& ) { header = default_value; } - catch ( const invalid_argument ) + catch ( const invalid_argument& ) { header = default_value; } @@ -208,11 +208,11 @@ namespace restbed { parameter = stof( get_query_parameter( name ) ); } - catch ( const out_of_range ) + catch ( const out_of_range& ) { parameter = default_value; } - catch ( const invalid_argument ) + catch ( const invalid_argument& ) { parameter = default_value; } @@ -228,11 +228,11 @@ namespace restbed { parameter = stod( get_query_parameter( name ) ); } - catch ( const out_of_range ) + catch ( const out_of_range& ) { parameter = default_value; } - catch ( const invalid_argument ) + catch ( const invalid_argument& ) { parameter = default_value; } @@ -277,11 +277,11 @@ namespace restbed { parameter = stof( get_path_parameter( name ) ); } - catch ( const out_of_range ) + catch ( const out_of_range& ) { parameter = default_value; } - catch ( const invalid_argument ) + catch ( const invalid_argument& ) { parameter = default_value; } @@ -297,11 +297,11 @@ namespace restbed { parameter = stod( get_path_parameter( name ) ); } - catch ( const out_of_range ) + catch ( const out_of_range& ) { parameter = default_value; } - catch ( const invalid_argument ) + catch ( const invalid_argument& ) { parameter = default_value; } diff --git a/html/js/config.js b/html/js/config.js index f5f1960..9864290 100755 --- a/html/js/config.js +++ b/html/js/config.js @@ -3,7 +3,7 @@ var config = { mainnetExplorerUrl: "https://xmrchain.com/", testnetExplorerUrl: "https://testnet.xmrchain.com/", stagenetExplorerUrl: "http://162.210.173.150:8083/", - nettype: 2, /* 0 - MAINNET, 1 - TESTNET, 2 - STAGENET */ + nettype: 0, /* 0 - MAINNET, 1 - TESTNET, 2 - STAGENET */ coinUnitPlaces: 12, txMinConfirms: 10, // corresponds to CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE in Monero txCoinbaseMinConfirms: 60, // corresponds to CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW in Monero diff --git a/src/CurrentBlockchainStatus.cpp b/src/CurrentBlockchainStatus.cpp index 3664dc5..378f33f 100755 --- a/src/CurrentBlockchainStatus.cpp +++ b/src/CurrentBlockchainStatus.cpp @@ -86,18 +86,8 @@ CurrentBlockchainStatus::init_monero_blockchain() uint32_t log_level = 0; mlog_configure(mlog_get_default_log_path(""), true); - mcore = unique_ptr(new xmreg::MicroCore{}); - // initialize the core using the blockchain path - if (!mcore->init(bc_setup.blockchain_path, bc_setup.net_type)) - return false; - - // get the high level Blockchain object to interact - // with the blockchain lmdb database - core_storage = &(mcore->get_core()); - - return true; -} + return mcore.init(bc_setup.blockchain_path, bc_setup.net_type);} bool @@ -153,7 +143,7 @@ CurrentBlockchainStatus::is_tx_spendtime_unlocked( bool CurrentBlockchainStatus::get_block(uint64_t height, block &blk) { - return mcore->get_block_from_height(height, blk); + return mcore.get_block_from_height(height, blk); } vector @@ -162,7 +152,7 @@ CurrentBlockchainStatus::get_blocks_range( { try { - return core_storage->get_db().get_blocks_range(h1, h2); + return mcore.get_blocks_range(h1, h2); } catch (BLOCK_DNE& e) { @@ -183,7 +173,7 @@ CurrentBlockchainStatus::get_block_txs( // the block i.e. coinbase tx. blk_txs.push_back(blk.miner_tx); - if (!core_storage->get_transactions(blk.tx_hashes, blk_txs, missed_txs)) + if (!mcore.get_transactions(blk.tx_hashes, blk_txs, missed_txs)) { cerr << "Cant get transactions in block: " << get_block_hash(blk) << endl; return false; @@ -198,7 +188,7 @@ CurrentBlockchainStatus::get_txs( vector& txs, vector& missed_txs) { - if (!core_storage->get_transactions(txs_to_get, txs, missed_txs)) + if (!mcore.get_transactions(txs_to_get, txs, missed_txs)) { cerr << "CurrentBlockchainStatus::get_txs: cant get transactions!\n"; return false; @@ -210,18 +200,13 @@ CurrentBlockchainStatus::get_txs( bool CurrentBlockchainStatus::tx_exist(const crypto::hash& tx_hash) { - return core_storage->have_tx(tx_hash); + return mcore.have_tx(tx_hash); } bool CurrentBlockchainStatus::tx_exist(const crypto::hash& tx_hash, uint64_t& tx_index) { - if (!core_storage->get_db().tx_exists(tx_hash, tx_index)) - { - return false; - } - - return true; + return mcore.tx_exists(tx_hash, tx_index); } @@ -263,8 +248,7 @@ CurrentBlockchainStatus::get_tx_with_output( { // get pair pair where first is tx hash // and second is local index of the output i in that tx - tx_out_idx = core_storage->get_db() - .get_output_tx_and_index(amount, output_idx); + tx_out_idx = mcore.get_output_tx_and_index(amount, output_idx); } catch (const OUTPUT_DNE &e) { @@ -281,7 +265,7 @@ CurrentBlockchainStatus::get_tx_with_output( output_idx_in_tx = tx_out_idx.second; - if (!mcore->get_tx(tx_out_idx.first, tx)) + if (!mcore.get_tx(tx_out_idx.first, tx)) { cerr << "Cant get tx: " << tx_out_idx.first << endl; @@ -298,9 +282,7 @@ CurrentBlockchainStatus::get_output_keys(const uint64_t& amount, { try { - core_storage->get_db().get_output_key(amount, - absolute_offsets, - outputs); + mcore.get_output_key(amount, absolute_offsets, outputs); } catch (const OUTPUT_DNE& e) { @@ -342,10 +324,9 @@ CurrentBlockchainStatus::get_amount_specific_indices(const crypto::hash& tx_hash // this index is lmdb index of a tx, not tx hash uint64_t tx_index; - if (core_storage->get_db().tx_exists(tx_hash, tx_index)) + if (mcore.tx_exists(tx_hash, tx_index)) { - out_indices = core_storage->get_db() - .get_tx_amount_output_indices(tx_index); + out_indices = mcore.get_tx_amount_output_indices(tx_index); return true; } @@ -434,9 +415,19 @@ CurrentBlockchainStatus::commit_tx(const string& tx_blob, string& error_msg, boo bool CurrentBlockchainStatus::read_mempool() { - rpccalls rpc {bc_setup.deamon_url}; + // get txs in the mempool + std::vector mempool_tx_info; + vector key_image_infos; - string error_msg; + if (mcore.get_mempool().get_transactions_and_spent_keys_info( + mempool_tx_info, key_image_infos)) + { + cerr << "Getting mempool failed " << endl; + return false; + } + + // not using this info at present + (void) key_image_infos; std::lock_guard lck (getting_mempool_txs); @@ -445,22 +436,13 @@ CurrentBlockchainStatus::read_mempool() // not very efficient but good enough for now. mempool_txs.clear(); - // get txs in the mempool - std::vector mempool_tx_info; - - if (!rpc.get_mempool(mempool_tx_info)) - { - cerr << "Getting mempool failed " << endl; - return false; - } - // if dont have tx_blob member, construct tx // from json obtained from the rpc call for (size_t i = 0; i < mempool_tx_info.size(); ++i) { // get transaction info of the tx in the mempool - tx_info _tx_info = mempool_tx_info.at(i); + tx_info const& _tx_info = mempool_tx_info.at(i); transaction tx; crypto::hash tx_hash; @@ -588,7 +570,7 @@ CurrentBlockchainStatus::search_if_payment_made( if (decrypted_payment_id8 != null_hash8) { - if (!mcore->get_device()->decrypt_payment_id( + if (!mcore.get_device()->decrypt_payment_id( decrypted_payment_id8, tx_pub_key, bc_setup.import_payment_viewkey)) { cerr << "Cant decrypt decrypted_payment_id8: " @@ -723,7 +705,7 @@ CurrentBlockchainStatus::get_payment_id_as_string(const transaction& tx) output_data_t CurrentBlockchainStatus::get_output_key(uint64_t amount, uint64_t global_amount_index) { - return core_storage->get_db().get_output_key(amount, global_amount_index); + return mcore.get_output_key(amount, global_amount_index); } bool @@ -943,7 +925,7 @@ CurrentBlockchainStatus::find_key_images_in_mempool(transaction const& tx) bool CurrentBlockchainStatus::get_tx(crypto::hash const& tx_hash, transaction& tx) { - return mcore->get_tx(tx_hash, tx); + return mcore.get_tx(tx_hash, tx); } @@ -957,7 +939,7 @@ CurrentBlockchainStatus::get_tx(string const& tx_hash_str, transaction& tx) return false; } - return mcore->get_tx(tx_hash, tx); + return mcore.get_tx(tx_hash, tx); } bool @@ -966,7 +948,7 @@ CurrentBlockchainStatus::get_tx_block_height(crypto::hash const& tx_hash, int64_ if (!tx_exist(tx_hash)) return false; - tx_height = core_storage->get_db().get_tx_block_height(tx_hash); + tx_height = mcore.get_tx_block_height(tx_hash); return true; } diff --git a/src/CurrentBlockchainStatus.h b/src/CurrentBlockchainStatus.h index 2998688..e9094f9 100755 --- a/src/CurrentBlockchainStatus.h +++ b/src/CurrentBlockchainStatus.h @@ -248,11 +248,10 @@ protected: // parameters used to connect/read monero blockchain BlockchainSetup bc_setup; - // since this class monitors current status + // since this class monitors current status // of the blockchain, its seems logical to // make object for accessing the blockchain here - unique_ptr mcore; - cryptonote::Blockchain *core_storage; + MicroCore mcore; // vector of mempool transactions that all threads // can refer to diff --git a/src/MicroCore.cpp b/src/MicroCore.cpp index bceaf4c..77b8a6d 100755 --- a/src/MicroCore.cpp +++ b/src/MicroCore.cpp @@ -19,11 +19,12 @@ namespace xmreg * * The same is done in cryptonode::core. */ -MicroCore::MicroCore(): - m_mempool(m_blockchain_storage), - m_blockchain_storage(m_mempool) +MicroCore::MicroCore(): + m_mempool(core_storage), + core_storage(m_mempool), + m_device {&hw::get_device("default")} { - m_device = &hw::get_device("default"); + } @@ -44,10 +45,18 @@ MicroCore::init(const string& _blockchain_path, network_type nt) nettype = nt; db_flags |= MDB_RDONLY; - db_flags |= MDB_NOLOCK; - BlockchainDB* db = nullptr; - db = new BlockchainLMDB(); + //m_mempool = make_unique(*core_storage); + + //core_storage.reset(new Blockchain(*m_mempool)); + + std::unique_ptr db(new_db("lmdb")); + + if (db == nullptr) + { + cerr << "Attempted to use non-existent database type\n"; + return false; + } try { @@ -67,27 +76,44 @@ MicroCore::init(const string& _blockchain_path, network_type nt) // initialize Blockchain object to manage // the database. - return m_blockchain_storage.init(db, nettype); + if (!core_storage.init(db.release(), nettype)) + { + cerr << "Error opening database: core_storage->init(db, nettype)\n" ; + return false; + } + + if (!m_mempool.init()) + { + cerr << "Error opening database: m_mempool.init()\n" ; + return false; + } + + return true; } /** * Get m_blockchain_storage. * Initialize m_blockchain_storage with the BlockchainLMDB object. */ -Blockchain& -MicroCore::get_core() +Blockchain const& +MicroCore::get_core() const { - return m_blockchain_storage; + return core_storage; } +tx_memory_pool const& +MicroCore::get_mempool() const +{ + return m_mempool; +} -bool + bool MicroCore::get_block_from_height(const uint64_t& height, block& blk) { try { - blk = m_blockchain_storage.get_db().get_block_from_height(height); + blk = core_storage.get_db().get_block_from_height(height); } catch (const exception& e) { @@ -106,10 +132,10 @@ MicroCore::get_block_from_height(const uint64_t& height, block& blk) bool MicroCore::get_tx(const crypto::hash& tx_hash, transaction& tx) { - if (m_blockchain_storage.have_tx(tx_hash)) + if (core_storage.have_tx(tx_hash)) { // get transaction with given hash - tx = m_blockchain_storage.get_db().get_tx(tx_hash); + tx = core_storage.get_db().get_tx(tx_hash); } else { @@ -121,43 +147,6 @@ MicroCore::get_tx(const crypto::hash& tx_hash, transaction& tx) return true; } - -/** - * De-initialized Blockchain. - * - * since blockchain is opened as MDB_RDONLY - * need to manually free memory taken on heap - * by BlockchainLMDB - */ -MicroCore::~MicroCore() -{ - delete &m_blockchain_storage.get_db(); -} - - -bool -init_blockchain(const string& path, - MicroCore& mcore, - Blockchain*& core_storage, - network_type nt) -{ - - // initialize the core using the blockchain path - if (!mcore.init(path, nt)) - { - cerr << "Error accessing blockchain." << endl; - return false; - } - - // get the high level Blockchain object to interact - // with the blockchain lmdb database - core_storage = &(mcore.get_core()); - - return true; -} - - - hw::device* const MicroCore::get_device() const { diff --git a/src/MicroCore.h b/src/MicroCore.h index db328b8..f775b01 100755 --- a/src/MicroCore.h +++ b/src/MicroCore.h @@ -29,8 +29,8 @@ class MicroCore { string blockchain_path; + Blockchain core_storage; tx_memory_pool m_mempool; - Blockchain m_blockchain_storage; hw::device* m_device; @@ -42,8 +42,59 @@ public: bool init(const string& _blockchain_path, network_type nt); - Blockchain& - get_core(); + Blockchain const& + get_core() const; + + tx_memory_pool const& + get_mempool() const; + + template + auto get_output_key(T&&... args) + { + return core_storage.get_db().get_output_key(std::forward(args)...); + } + + template + auto get_transactions(T&&... args) + { + return core_storage.get_transactions(std::forward(args)...); + } + + template + auto get_blocks_range(T&&... args) + { + return core_storage.get_db().get_blocks_range(std::forward(args)...); + } + + template + auto have_tx(T&&... args) + { + return core_storage.have_tx(std::forward(args)...); + } + + template + auto tx_exists(T&&... args) + { + return core_storage.get_db().tx_exists(std::forward(args)...); + } + + template + auto get_output_tx_and_index(T&&... args) + { + return core_storage.get_db().get_output_tx_and_index(std::forward(args)...); + } + + template + auto get_tx_block_height(T&&... args) + { + return core_storage.get_db().get_tx_block_height(std::forward(args)...); + } + + template + auto get_tx_amount_output_indices(T&&... args) + { + return core_storage.get_db().get_tx_amount_output_indices(std::forward(args)...); + } bool get_block_from_height(const uint64_t& height, block& blk); @@ -54,20 +105,8 @@ public: hw::device* const get_device() const; - virtual ~MicroCore(); - }; - - - -bool -init_blockchain(const string& path, - MicroCore& mcore, - Blockchain*& core_storage, - network_type nt); - - } diff --git a/src/rpccalls.cpp b/src/rpccalls.cpp index 332055c..f5a287b 100755 --- a/src/rpccalls.cpp +++ b/src/rpccalls.cpp @@ -67,39 +67,6 @@ rpccalls::get_current_height() return res.height; } -bool -rpccalls::get_mempool(vector& mempool_txs) -{ - - COMMAND_RPC_GET_TRANSACTION_POOL::request req; - COMMAND_RPC_GET_TRANSACTION_POOL::response res; - - std::lock_guard guard(m_daemon_rpc_mutex); - - if (!connect_to_monero_deamon()) - { - cerr << "get_current_height: not connected to deamon" << endl; - return false; - } - - bool r = epee::net_utils::invoke_http_json( - "/get_transaction_pool", - req, res, m_http_client, timeout_time_ms); - - if (!r) - { - cerr << "Error connecting to Monero deamon at " - << deamon_url << endl; - return false; - } - - - mempool_txs = res.transactions; - - return true; -} - - bool rpccalls::get_random_outs_for_amounts( const vector& amounts, @@ -293,4 +260,4 @@ rpccalls::commit_tx( } -} \ No newline at end of file +} diff --git a/src/rpccalls.h b/src/rpccalls.h index a6f426b..363a03f 100755 --- a/src/rpccalls.h +++ b/src/rpccalls.h @@ -42,9 +42,6 @@ public: uint64_t get_current_height(); - bool - get_mempool(vector& mempool_txs); - bool get_random_outs_for_amounts( const vector& amounts, diff --git a/tests/mysql_tests.cpp b/tests/mysql_tests.cpp index a4155b2..16d84e6 100644 --- a/tests/mysql_tests.cpp +++ b/tests/mysql_tests.cpp @@ -1619,4 +1619,4 @@ TEST_F(MYSQL_TEST, MysqlPingThreadStopsOnPointerExpiry) // //} -} \ No newline at end of file +}