From 355727ba3cd171995cd33065421c35df8b3ceac6 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Fri, 16 Dec 2016 12:41:24 +0800 Subject: [PATCH] TxSearch threads writes outputs to Outputs table when found. --- src/MySqlAccounts.h | 23 +++++++++++++++--- src/TxSearch.h | 59 +++++++++++++++++++++++++++++++-------------- src/ssqlses.h | 19 ++++++++++----- 3 files changed, 74 insertions(+), 27 deletions(-) diff --git a/src/MySqlAccounts.h b/src/MySqlAccounts.h index 9fd60ac..4957414 100644 --- a/src/MySqlAccounts.h +++ b/src/MySqlAccounts.h @@ -100,6 +100,7 @@ public: { SimpleResult sr = query.execute(out_data.account_id, out_data.tx_id, + out_data.out_pub_key, out_data.tx_pub_key, out_data.out_index, out_data.mixin, @@ -253,14 +254,17 @@ class MySqlAccounts shared_ptr mysql_tx; + shared_ptr mysql_out; + public: MySqlAccounts() { - cout << "MySqlAccounts() makes new connection" << endl; - conn = make_shared(); - mysql_tx = make_shared(conn); + //cout << "MySqlAccounts() makes new connection" << endl; + conn = make_shared(); + mysql_tx = make_shared(conn); + mysql_out = make_shared(conn); } @@ -382,6 +386,12 @@ public: return mysql_tx->insert(tx_data); } + uint64_t + insert_output(const XmrOutput& tx_out) + { + return mysql_out->insert(tx_out); + } + bool select_txs(const string& xmr_address, vector& txs) { @@ -408,6 +418,13 @@ public: } + bool + select_outputs(const uint64_t& account_id, vector& outs) + { + return mysql_out->select(account_id, outs); + } + + uint64_t get_total_recieved(const uint64_t& account_id) { diff --git a/src/TxSearch.h b/src/TxSearch.h index 82fa5fa..024735f 100644 --- a/src/TxSearch.h +++ b/src/TxSearch.h @@ -205,7 +205,8 @@ public: uint64_t total_received {0}; - bool found_mine_outputs {false}; + // + vector> found_mine_outputs; for (auto& out: outputs) { @@ -266,31 +267,33 @@ public: if (mine_output) { + + string out_key_str = pod_to_hex(txout_k.key); + // found an output associated with the given address and viewkey string msg = fmt::format("block: {:d}, tx_hash: {:s}, output_pub_key: {:s}\n", searched_blk_no, pod_to_hex(tx_hash), - pod_to_hex(txout_k.key)); + out_key_str); cout << msg << endl; total_received += amount; - found_mine_outputs = true; + found_mine_outputs.emplace_back(out_key_str, output_idx_in_tx); } } // for (const auto& out: outputs) - if (found_mine_outputs) - { + if (!found_mine_outputs.empty()) { - crypto::hash payment_id = null_hash; + crypto::hash payment_id = null_hash; crypto::hash8 payment_id8 = null_hash8; get_payment_id(tx, payment_id, payment_id8); - string payment_id_str {""}; + string payment_id_str{""}; if (payment_id != null_hash) { @@ -301,25 +304,45 @@ public: payment_id_str = pod_to_hex(payment_id8); } + string tx_hash_str = pod_to_hex(tx_hash); + XmrTransaction tx_data; - tx_data.hash = pod_to_hex(tx_hash); - tx_data.account_id = acc.id; + tx_data.hash = tx_hash_str; + tx_data.account_id = acc.id; tx_data.total_received = total_received; - tx_data.total_sent = 0; // at this stage we don't have any - // info about spendings - tx_data.unlock_time = 0; - tx_data.height = searched_blk_no; - tx_data.coinbase = is_coinbase(tx); - tx_data.payment_id = payment_id_str; - tx_data.mixin = get_mixin_no(tx) - 1; - tx_data.timestamp = XmrTransaction::timestamp_to_DateTime(blk.timestamp); + tx_data.total_sent = 0; // at this stage we don't have any + // info about spendings + tx_data.unlock_time = 0; + tx_data.height = searched_blk_no; + tx_data.coinbase = is_coinbase(tx); + tx_data.payment_id = payment_id_str; + tx_data.mixin = get_mixin_no(tx) - 1; + tx_data.timestamp = XmrTransaction::timestamp_to_DateTime(blk.timestamp); // insert tx_data into mysql's Transactions table uint64_t tx_mysql_id = xmr_accounts->insert_tx(tx_data); + // now add the found outputs into Outputs tables + + for (auto &out_k_idx: found_mine_outputs) + { + XmrOutput out_data; + + out_data.account_id = acc.id; + out_data.tx_id = tx_mysql_id; + out_data.out_pub_key = out_k_idx.first; + out_data.tx_pub_key = pod_to_hex(tx_pub_key); + out_data.out_index = out_k_idx.second; + out_data.mixin = tx_data.mixin; + out_data.timestamp = tx_data.timestamp; + + // insert output into mysql's outputs table + uint64_t out_mysql_id = xmr_accounts->insert_output(out_data); + } + - // once tx was added, update Accounts table + // once tx and outputs were added, update Accounts table XmrAccount updated_acc = acc; diff --git a/src/ssqlses.h b/src/ssqlses.h index 956c8b8..67ae0fb 100644 --- a/src/ssqlses.h +++ b/src/ssqlses.h @@ -177,10 +177,11 @@ ostream& operator<< (std::ostream& os, const XmrTransaction& acc) { }; -sql_create_7(Outputs, 1, 3, +sql_create_8(Outputs, 1, 3, sql_bigint_unsigned, id, sql_bigint_unsigned, account_id, sql_bigint_unsigned, tx_id, + sql_varchar , out_pub_key, sql_varchar , tx_pub_key, sql_bigint_unsigned, out_index, sql_bigint_unsigned, mixin, @@ -191,14 +192,19 @@ struct XmrOutput : public Outputs { static constexpr const char* SELECT_STMT = R"( - SELECT * FROM `Outputs` WHERE `account_id` = (%0q) + SELECT * FROM `Outputs` WHERE `account_id` = (%0q) + )"; + + static constexpr const char* EXIST_STMT = R"( + SELECT 1 FROM `Outputs` WHERE `out_pub_key` == (%0q) )"; static constexpr const char* INSERT_STMT = R"( - INSERT IGNORE INTO `Outputs` (`account_id`, `tx_id`, `tx_pub_key`, `out_index`, `mixin`, `timestamp`) - VALUES (%0q, %1q, %2q, - %3q, %4q, %5q); -)"; + INSERT IGNORE INTO `Outputs` (`account_id`, `tx_id`, `out_pub_key`, `tx_pub_key`, + `out_index`, `mixin`, `timestamp`) + VALUES (%0q, %1q, %2q, %3q, + %4q, %5q, %6q); + )"; @@ -210,6 +216,7 @@ struct XmrOutput : public Outputs json j {{"id" , id}, {"account_id" , account_id}, {"tx_id" , tx_id}, + {"out_pub_key" , out_pub_key}, {"tx_pub_key" , tx_pub_key}, {"out_index" , out_index}, {"mixin" , mixin},