From 3ef2f662a80f77b6726a5b768187770c8bc0eb9f Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Wed, 11 May 2016 06:32:02 +0000 Subject: [PATCH] mylmdb and tools updated --- src/mylmdb.h | 89 ++++++++++++++++++++++++++++++++------------------- src/tools.cpp | 22 +++++++++++++ src/tools.h | 3 ++ 3 files changed, 81 insertions(+), 33 deletions(-) diff --git a/src/mylmdb.h b/src/mylmdb.h index 78bf8b6..c675616 100644 --- a/src/mylmdb.h +++ b/src/mylmdb.h @@ -17,6 +17,22 @@ namespace xmreg using namespace std; + + /** + * Stores info about outputs useful + * for checking which ouputs belong to a + * given address and viewkey + */ + struct output_info + { + crypto::hash tx_hash; + crypto::public_key tx_pub_key; + uint64_t amount; + uint64_t index_in_tx; + }; + + + class MyLMDB { @@ -118,14 +134,17 @@ namespace xmreg { crypto::hash tx_hash = get_transaction_hash(tx); + crypto::public_key tx_pub_key = get_tx_pub_key_from_extra(tx); + string tx_hash_str = pod_to_hex(tx_hash); - vector> outputs - = xmreg::get_ouputs(tx); + vector> outputs = + xmreg::get_ouputs_tuple(tx); lmdb::txn wtxn {nullptr}; lmdb::dbi wdbi1 {0}; lmdb::dbi wdbi2 {0}; + lmdb::dbi wdbi3 {0}; unsigned int flags = MDB_CREATE | MDB_DUPSORT | MDB_DUPFIXED; @@ -134,6 +153,7 @@ namespace xmreg wtxn = lmdb::txn::begin(m_env); wdbi1 = lmdb::dbi::open(wtxn, "output_public_keys", flags); wdbi2 = lmdb::dbi::open(wtxn, "output_amounts", flags); + wdbi3 = lmdb::dbi::open(wtxn, "output_info", flags); } catch (lmdb::error& e ) { @@ -141,18 +161,28 @@ namespace xmreg return false; } - for (pair& output: outputs) + for (auto& output: outputs) { - string public_key_str = pod_to_hex(output.first.key); + string public_key_str = pod_to_hex(std::get<0>(output).key); lmdb::val public_key_val {public_key_str}; lmdb::val tx_hash_val {tx_hash_str}; - lmdb::val amount_val {static_cast(&output.second), - sizeof(output.second)}; + uint64_t amount = std::get<1>(output); + + lmdb::val amount_val {static_cast(&amount), sizeof(amount)}; + + uint64_t index_in_tx = std::get<2>(output); + + + output_info out_info {tx_hash, tx_pub_key, amount, index_in_tx}; + + + lmdb::val out_info_val {static_cast(&out_info), sizeof(out_info)}; wdbi1.put(wtxn, public_key_val, tx_hash_val); wdbi2.put(wtxn, public_key_val, amount_val); + wdbi3.put(wtxn, public_key_val, out_info_val); } try @@ -274,6 +304,10 @@ 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}; @@ -375,51 +409,40 @@ namespace xmreg } - void - for_all_output_amounts(std::function f) + bool + get_output_info(const string& key, + output_info& out_info, + const string& db_name = "output_info") { - unsigned int flags = MDB_DUPSORT | MDB_DUPFIXED; + + unsigned int flags = 0; try { lmdb::txn rtxn = lmdb::txn::begin(m_env, nullptr, MDB_RDONLY); - lmdb::dbi rdbi = lmdb::dbi::open(rtxn, "output_amounts", flags); - lmdb::cursor cr = lmdb::cursor::open(rtxn, rdbi); - - lmdb::val key_to_find; - lmdb::val amount_val; + lmdb::dbi rdbi = lmdb::dbi::open(rtxn, db_name.c_str(), flags); + lmdb::val key_to_find{key}; + lmdb::val info_val; - // process all values for the same key - while (cr.get(key_to_find, amount_val, MDB_NEXT)) + if(!rdbi.get(rtxn, key_to_find, info_val)) { - public_key pub_key; - - epee::string_tools::hex_to_pod( - string(key_to_find.data(), key_to_find.size()), - pub_key); - - uint64_t xmr_amount = *(amount_val.data()); - - //cout << key_val_to_str(key_to_find, tx_hash_val) << endl; - - if (f(pub_key, xmr_amount) == false) - { - break; - } + return false; } - cr.close(); + out_info = *(info_val.data()); + rtxn.abort(); } catch (lmdb::error& e) { cerr << e.what() << endl; + return false; } + + return true; } void diff --git a/src/tools.cpp b/src/tools.cpp index 4578536..586db04 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -372,7 +372,29 @@ namespace xmreg return outputs; + }; + + vector> + get_ouputs_tuple(const transaction& tx) + { + vector> outputs; + + for (uint64_t n = 0; n < tx.vout.size(); ++n) + { + if (tx.vout[n].target.type() != typeid(txout_to_key)) + { + continue; + } + + // get tx input key + const txout_to_key& txout_key + = boost::get(tx.vout[n].target); + + outputs.push_back(make_tuple(txout_key, tx.vout[n].amount, n)); + } + + return outputs; }; uint64_t diff --git a/src/tools.h b/src/tools.h index 2f06403..0f1bb7e 100644 --- a/src/tools.h +++ b/src/tools.h @@ -155,6 +155,9 @@ namespace xmreg vector> get_ouputs(const transaction& tx); + vector> + get_ouputs_tuple(const transaction& tx); + vector get_key_images(const transaction& tx);