From 0aaf3580f61626e133870eb93dc7c447d48ee27e Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Tue, 14 Feb 2017 23:22:00 +0000 Subject: [PATCH] find_txs_in_mempool method moved to TxSearch class --- html/js/config.js | 2 +- src/CurrentBlockchainStatus.cpp | 22 +++++++++++- src/CurrentBlockchainStatus.h | 3 ++ src/TxSearch.cpp | 55 +++++++++++++++++++++++++++-- src/TxSearch.h | 28 +++++++++++++++ src/YourMoneroRequests.cpp | 62 ++------------------------------- src/YourMoneroRequests.h | 26 -------------- 7 files changed, 109 insertions(+), 89 deletions(-) diff --git a/html/js/config.js b/html/js/config.js index cb7f044..84275c8 100755 --- a/html/js/config.js +++ b/html/js/config.js @@ -1,6 +1,6 @@ var config = { apiUrl: "http://127.0.0.1:1984/", - testnet: false, + testnet: true, coinUnitPlaces: 12, txMinConfirms: 10, coinSymbol: 'XMR', diff --git a/src/CurrentBlockchainStatus.cpp b/src/CurrentBlockchainStatus.cpp index ed8f20f..fa20926 100644 --- a/src/CurrentBlockchainStatus.cpp +++ b/src/CurrentBlockchainStatus.cpp @@ -621,8 +621,28 @@ CurrentBlockchainStatus::get_xmr_address_viewkey( return true; }; - bool +CurrentBlockchainStatus::find_txs_in_mempool( + const string& address_str, + json& transactions) +{ + std::lock_guard lck (searching_threads_map_mtx); + + if (searching_threads.count(address_str) == 0) + { + // thread does not exist + cout << "thread for " << address_str << " does not exist" << endl; + return false; + } + + transactions = searching_threads[address_str].get() + ->find_txs_in_mempool(mempool_txs); + + return true; +}; + + + bool CurrentBlockchainStatus::set_new_searched_blk_no(const string& address, uint64_t new_value) { std::lock_guard lck (searching_threads_map_mtx); diff --git a/src/CurrentBlockchainStatus.h b/src/CurrentBlockchainStatus.h index da69923..69ba598 100644 --- a/src/CurrentBlockchainStatus.h +++ b/src/CurrentBlockchainStatus.h @@ -156,6 +156,9 @@ struct CurrentBlockchainStatus get_xmr_address_viewkey(const string& address_str, account_public_address& address, secret_key& viewkey); + static bool + find_txs_in_mempool(const string& address_str, + json& transactions); static bool set_new_searched_blk_no(const string& address, diff --git a/src/TxSearch.cpp b/src/TxSearch.cpp index 8fc252d..9318039 100644 --- a/src/TxSearch.cpp +++ b/src/TxSearch.cpp @@ -451,11 +451,62 @@ TxSearch::populate_known_outputs() { known_outputs_keys.push_back(make_pair(out.out_pub_key, out.amount)); } - } } -pair + +json +TxSearch::find_txs_in_mempool(vector mempool_txs) +{ + json j_transactions = json::array(); + + uint64_t current_height = CurrentBlockchainStatus::get_current_blockchain_height(); + + for (const transaction& tx: mempool_txs) + { + // Class that is resposnible for idenficitaction of our outputs + // and inputs in a given tx. + OutputInputIdentification oi_identification {&address, &viewkey, &tx}; + + // FIRSt step. to search for the incoming xmr, we use address, viewkey and + // outputs public key. + oi_identification.identify_outputs(); + + //vector amount_specific_indices; + + // if we identified some outputs as ours, + // save them into json to be returned. + if (!oi_identification.identified_outputs.empty()) + { + json j_tx; + + j_tx["id"] = 0; + j_tx["hash"] = oi_identification.tx_hash_str; + j_tx["timestamp"] = ""; + j_tx["total_received"] = oi_identification.total_received; + j_tx["total_sent"] = 0; + j_tx["unlock_time"] = 0; + j_tx["height"] = current_height; // put large value of height, + // just to indicate that we dont have + // height and that in frontend it will + // appear us unconfirmed. + j_tx["payment_id"] = ""; + j_tx["coinbase"] = false; + j_tx["mixin"] = get_mixin_no(tx) - 1; + + j_transactions.push_back(j_tx); + } + + } // for (const transaction& tx: txs_to_check) + + return j_transactions; + +} + + + + + pair TxSearch::get_xmr_address_viewkey() const { return make_pair(address, viewkey); diff --git a/src/TxSearch.h b/src/TxSearch.h index 4d5660c..cc0005d 100644 --- a/src/TxSearch.h +++ b/src/TxSearch.h @@ -91,6 +91,34 @@ public: void populate_known_outputs(); + + /** + * Search for our txs in the mempool + * + * The method searches for our txs (outputs and inputs) + * in the mempool. It does basically same what search method + * The difference is that search method searches in a thread + * in a blockchain. It does not scan for tx in mempool. This is because + * it writes what it finds into database for permament storage. + * However txs in mempool are not permament. Also since we want to + * give the end user quick update on incoming/outging tx, this method + * will be executed whenever frontend wants. By default it is every + * 10 seconds. TxSearch class is timed independetly of the frontend. + * Also since we dont write here anything to the database, we + * return a json that will be appended to json produced by get_address_tx + * and similar function. The outputs here cant be spent anyway. This is + * only for end user information. The txs found here will be written + * to database later on by TxSearch thread when they will be added + * to the blockchain. + * + * we pass mempool_txs by copy because we want copy of mempool txs. + * to avoid worrying about synchronizing threads + * + * @return json + */ + json + find_txs_in_mempool(vector mempool_txs); + pair get_xmr_address_viewkey() const; diff --git a/src/YourMoneroRequests.cpp b/src/YourMoneroRequests.cpp index 8019178..59b841a 100644 --- a/src/YourMoneroRequests.cpp +++ b/src/YourMoneroRequests.cpp @@ -231,14 +231,11 @@ YourMoneroRequests::get_address_txs(const shared_ptr< Session > session, const B // append txs found in mempool to the json returned - account_public_address address_parsed; - secret_key viewkey_parsed; + json j_mempool_tx; - if (CurrentBlockchainStatus::get_xmr_address_viewkey( - xmr_address, address_parsed, viewkey_parsed)) + if (CurrentBlockchainStatus::find_txs_in_mempool( + xmr_address, j_mempool_tx)) { - json j_mempool_tx = find_txs_in_mempool(&address_parsed, &viewkey_parsed); - if(!j_mempool_tx.empty()) { uint64_t total_received_mempool {0}; @@ -779,59 +776,6 @@ YourMoneroRequests::get_current_blockchain_height() } -json -YourMoneroRequests::find_txs_in_mempool( - const account_public_address* address, - const secret_key* viewkey) -{ - vector txs_to_check = CurrentBlockchainStatus::get_mempool_txs(); - - json j_transactions = json::array(); - - uint64_t current_height = CurrentBlockchainStatus::get_current_blockchain_height(); - - for (const transaction& tx: txs_to_check) - { - // Class that is resposnible for idenficitaction of our outputs - // and inputs in a given tx. - OutputInputIdentification oi_identification {address, viewkey, &tx}; - - // FIRSt step. to search for the incoming xmr, we use address, viewkey and - // outputs public key. - oi_identification.identify_outputs(); - - //vector amount_specific_indices; - - // if we identified some outputs as ours, - // save them into json to be returned. - if (!oi_identification.identified_outputs.empty()) - { - json j_tx; - - j_tx["id"] = 0; - j_tx["hash"] = oi_identification.tx_hash_str; - j_tx["timestamp"] = get_current_time(); - j_tx["total_received"] = oi_identification.total_received; - j_tx["total_sent"] = 0; - j_tx["unlock_time"] = 0; - j_tx["height"] = current_height; // put large value of height, - // just to indicate that we dont have - // height and that in frontend it will - // appear us unconfirmed. - j_tx["payment_id"] = ""; - j_tx["coinbase"] = false; - j_tx["mixin"] = get_mixin_no(tx) - 1; - - j_transactions.push_back(j_tx); - } - - } // for (const transaction& tx: txs_to_check) - - return j_transactions; - -} - - // define static variables bool YourMoneroRequests::show_logs = false; } diff --git a/src/YourMoneroRequests.h b/src/YourMoneroRequests.h index cd64bfb..90f0162 100644 --- a/src/YourMoneroRequests.h +++ b/src/YourMoneroRequests.h @@ -107,32 +107,6 @@ public: inline uint64_t get_current_blockchain_height(); -private: - - /** - * Search for our txs in the mempool - * - * The method searches for our txs (outputs and inputs) - * in the mempool. It does basically same what TxSearch class is - * doing. The difference is that TxSearch class searches in a thread - * in a blockchain. It does not scan for tx in mempool. This is because - * TxSearch writes what it finds into database for permament storage. - * However txs in mempool are not permament. Also since we want to - * give the end user quick update on incoming/outging tx, this method - * will be executed whenever frontend wants. By default it is every - * 10 seconds. TxSearch class is timed independetly of the frontend. - * Also since we dont write here anything to the database, we - * return a json that will be appended to json produced by get_address_tx - * and similar function. The outputs here cant be spent anyway. This is - * only for end user information. The txs found here will be written - * to database later on by TxSearch thread when they will be added - * to the blockchain. - * - * @return json - */ - json - find_txs_in_mempool(const account_public_address* address, - const secret_key* viewkey); };