From 877eface3f801dc29b83180eca0e33d73fd4edf0 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Fri, 6 May 2016 08:00:57 +0000 Subject: [PATCH] searching mempool added, but needs testing --- src/mylmdb.h | 6 +- src/page.h | 156 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 138 insertions(+), 24 deletions(-) diff --git a/src/mylmdb.h b/src/mylmdb.h index 825d9fa..ca1071e 100644 --- a/src/mylmdb.h +++ b/src/mylmdb.h @@ -274,10 +274,6 @@ namespace xmreg lmdb::txn wtxn = lmdb::txn::begin(m_env); lmdb::dbi wdbi = lmdb::dbi::open(wtxn, "encrypted_payments_id", flags); - //cout << "Saving encrypted payiment_id: " << payment_id_str << endl; - //string wait_for_enter; - //cin >> wait_for_enter; - lmdb::val payment_id_val {payment_id_str}; lmdb::val tx_hash_val {tx_hash_str}; @@ -303,7 +299,7 @@ namespace xmreg try { - + lmdb::txn rtxn = lmdb::txn::begin(m_env, nullptr, MDB_RDONLY); lmdb::dbi rdbi = lmdb::dbi::open(rtxn, db_name.c_str(), flags); lmdb::cursor cr = lmdb::cursor::open(rtxn, rdbi); diff --git a/src/page.h b/src/page.h index 6151e87..b267eb8 100644 --- a/src/page.h +++ b/src/page.h @@ -851,7 +851,7 @@ namespace xmreg { if (!mcore->get_tx(tx_hash, tx)) { cerr << "Cant get tx in blockchain: " << tx_hash - << ". Check mempool now" << endl; + << ". \n Check mempool now" << endl; vector> found_txs = search_mempool(tx_hash); @@ -1153,38 +1153,156 @@ namespace xmreg { // for key_images, public_keys, payments_id, etc. vector mempool_txs = get_mempool_txs(); + // key is string indicating where search_text was found. + map> tx_search_results + = search_txs(mempool_txs, search_text); + // now search my own custom lmdb database // with key_images, public_keys, payments_id etc. vector>> all_possible_tx_hashes; - xmreg::MyLMDB mylmdb {mcore->get_blkchain_path()}; + //@TODO make lmdb2 path to some option + xmreg::MyLMDB mylmdb {"/home/mwo/.bitmonero/lmdb2"}; + + // search the custum lmdb for key_images and append the result + // to those from the mempool search if found + + mylmdb.search(search_text, + tx_search_results["key_images"], + "key_images"); + + cout << "size: " << tx_search_results["key_images"].size() << endl; + + all_possible_tx_hashes.push_back( + make_pair("key_images", + tx_search_results["key_images"])); + + + // search the custum lmdb for tx_public_keys and append the result + // to those from the mempool search if found + + mylmdb.search(search_text, + tx_search_results["tx_public_keys"], + "tx_public_keys"); + + all_possible_tx_hashes.push_back( + make_pair("tx_public_keys", + tx_search_results["tx_public_keys"])); + + // search the custum lmdb for payments_id and append the result + // to those from the mempool search if found + + mylmdb.search(search_text, + tx_search_results["payments_id"], + "payments_id"); + + all_possible_tx_hashes.push_back( + make_pair("payments_id", + tx_search_results["payments_id"])); + + // search the custum lmdb for encrypted_payments_id and append the result + // to those from the mempool search if found - vector tx_hashes; - mylmdb.search(search_text, tx_hashes, "key_images"); - all_possible_tx_hashes.push_back(make_pair("key_images", tx_hashes)); + mylmdb.search(search_text, + tx_search_results["encrypted_payments_id"], + "encrypted_payments_id"); - tx_hashes.clear(); - mylmdb.search(search_text, tx_hashes, "tx_public_keys"); - all_possible_tx_hashes.push_back(make_pair("tx_public_keys", tx_hashes)); + all_possible_tx_hashes.push_back( + make_pair("encrypted_payments_id", + tx_search_results["encrypted_payments_id"])); - tx_hashes.clear(); - mylmdb.search(search_text, tx_hashes, "payments_id"); - all_possible_tx_hashes.push_back(make_pair("payments_id", tx_hashes)); + // search the custum lmdb for output_public_keys and append the result + // to those from the mempool search if found - tx_hashes.clear(); - mylmdb.search(search_text, tx_hashes, "encrypted_payments_id"); - all_possible_tx_hashes.push_back(make_pair("encrypted_payments_id", tx_hashes)); + mylmdb.search(search_text, + tx_search_results["output_public_keys"], + "output_public_keys"); - tx_hashes.clear(); - mylmdb.search(search_text, tx_hashes, "output_public_keys"); - all_possible_tx_hashes.push_back(make_pair("output_public_keys", tx_hashes)); + all_possible_tx_hashes.push_back( + make_pair("output_public_keys", + tx_search_results["output_public_keys"])); result_html = show_search_results(search_text, all_possible_tx_hashes); return result_html; } + + map> + search_txs(vector txs, const string& search_text) + { + map> tx_hashes; + + // initlizte the map with empty results + tx_hashes["key_images"] = {}; + tx_hashes["tx_public_keys"] = {}; + tx_hashes["payments_id"] = {}; + tx_hashes["encrypted_payments_id"] = {}; + tx_hashes["output_public_keys"] = {}; + + for (const transaction& tx: txs) + { + + tx_details txd = get_tx_details(tx); + + string tx_hash_str = pod_to_hex(txd.hash); + + // check if any key_image matches the search_text + + vector::iterator it1 = + find_if(begin(txd.input_key_imgs), end(txd.input_key_imgs), + [&](const txin_to_key& key_img) + { + return pod_to_hex(key_img.k_image) == search_text; + }); + + if (it1 != txd.input_key_imgs.end()) + { + tx_hashes["key_images"].push_back(tx_hash_str); + } + + // check if tx_public_key matches the search_text + + if (pod_to_hex(txd.pk) == search_text) + { + tx_hashes["tx_public_keys"].push_back(tx_hash_str); + } + + // check if payments_id matches the search_text + + if (pod_to_hex(txd.payment_id) == search_text) + { + tx_hashes["payment_id"].push_back(tx_hash_str); + } + + // check if encrypted_payments_id matches the search_text + + if (pod_to_hex(txd.payment_id8) == search_text) + { + tx_hashes["encrypted_payments_id"].push_back(tx_hash_str); + } + + // check if output_public_keys matche the search_text + + vector>::iterator it2 = + find_if(begin(txd.output_pub_keys), end(txd.output_pub_keys), + [&](const pair& tx_out_pk) + { + return pod_to_hex(tx_out_pk.first.key) == search_text; + }); + + if (it2 != txd.output_pub_keys.end()) + { + tx_hashes["output_public_keys"].push_back(tx_hash_str); + } + + } + + return tx_hashes; + + } + vector get_mempool_txs() { @@ -1240,7 +1358,7 @@ namespace xmreg { if (!mcore->get_tx(tx_hash, tx)) { - return string("Cant get tx of hash: " + tx_hash); + return string("Cant get tx of hash (show_search_results): " + tx_hash); } tx_details txd = get_tx_details(tx); @@ -1322,7 +1440,7 @@ namespace xmreg { txd.xmr_outputs = sum_money_in_outputs(tx); // get mixin number - txd.mixin_no = get_mixin_no(tx); + txd.mixin_no = get_mixin_no(tx); if (!coinbase && tx.vin.size() > 1) {