From 16b8c429bf8b67a3874e1e8ed8c576c916c1b935 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Sat, 25 Mar 2017 15:42:45 +0800 Subject: [PATCH] blockchain_tx_id added to Transaction table --- sql/openmonero.sql | 1 + src/CurrentBlockchainStatus.cpp | 12 +++++ src/CurrentBlockchainStatus.h | 3 ++ src/MySqlAccounts.cpp | 1 + src/OutputInputIdentification.cpp | 2 +- src/TxSearch.cpp | 84 ++++++++++++++++++------------- src/YourMoneroRequests.cpp | 2 +- src/ssqlses.h | 15 +++--- 8 files changed, 76 insertions(+), 44 deletions(-) diff --git a/sql/openmonero.sql b/sql/openmonero.sql index ad96f61..9fd0e2b 100644 --- a/sql/openmonero.sql +++ b/sql/openmonero.sql @@ -110,6 +110,7 @@ CREATE TABLE `Transactions` ( `hash` varchar(64) NOT NULL, `prefix_hash` varchar(64) NOT NULL DEFAULT '', `account_id` bigint(20) UNSIGNED NOT NULL, + `blockchain_tx_id` bigint(20) UNSIGNED NOT NULL, `total_received` bigint(20) UNSIGNED NOT NULL, `total_sent` bigint(20) UNSIGNED NOT NULL, `unlock_time` bigint(20) UNSIGNED NOT NULL DEFAULT '0', diff --git a/src/CurrentBlockchainStatus.cpp b/src/CurrentBlockchainStatus.cpp index 9d85e79..7b3b511 100644 --- a/src/CurrentBlockchainStatus.cpp +++ b/src/CurrentBlockchainStatus.cpp @@ -224,6 +224,18 @@ CurrentBlockchainStatus::tx_exist(const crypto::hash& tx_hash) return core_storage->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; +} + + bool CurrentBlockchainStatus::tx_exist(const string& tx_hash_str) { diff --git a/src/CurrentBlockchainStatus.h b/src/CurrentBlockchainStatus.h index 92b8929..7aa0c12 100644 --- a/src/CurrentBlockchainStatus.h +++ b/src/CurrentBlockchainStatus.h @@ -103,6 +103,9 @@ struct CurrentBlockchainStatus static bool tx_exist(const crypto::hash& tx_hash); + static bool + tx_exist(const crypto::hash& tx_hash, uint64_t& tx_index); + static bool tx_exist(const string& tx_hash_str); diff --git a/src/MySqlAccounts.cpp b/src/MySqlAccounts.cpp index 8bcabb3..9b2d8f2 100644 --- a/src/MySqlAccounts.cpp +++ b/src/MySqlAccounts.cpp @@ -398,6 +398,7 @@ MysqlTransactions::insert(const XmrTransaction& tx_data) SimpleResult sr = query.execute(tx_data.hash, tx_data.prefix_hash, tx_data.account_id, + tx_data.blockchain_tx_id, tx_data.total_received, tx_data.total_sent, tx_data.unlock_time, diff --git a/src/OutputInputIdentification.cpp b/src/OutputInputIdentification.cpp index 75eb4d4..b42247d 100644 --- a/src/OutputInputIdentification.cpp +++ b/src/OutputInputIdentification.cpp @@ -20,7 +20,7 @@ OutputInputIdentification::OutputInputIdentification( tx_hash = get_transaction_hash(*tx); tx_prefix_hash = get_transaction_prefix_hash(*tx); - tx_pub_key = xmreg::get_tx_pub_key_from_received_outs(*tx); + tx_pub_key = xmreg::get_tx_pub_key_from_received_outs(*tx); tx_hash_str = pod_to_hex(tx_hash); tx_prefix_hash_str = pod_to_hex(tx_prefix_hash); diff --git a/src/TxSearch.cpp b/src/TxSearch.cpp index 989695a..ff6ae23 100644 --- a/src/TxSearch.cpp +++ b/src/TxSearch.cpp @@ -169,6 +169,17 @@ TxSearch::search() bool is_spendable = CurrentBlockchainStatus::is_tx_unlocked( tx.unlock_time, searched_blk_no); + + // this is id of txs in lmdb blockchain table. + // it will be used mostly to sort txs in the frontend. + uint64_t blockchain_tx_id {0}; + + if (!CurrentBlockchainStatus::tx_exist(oi_identification.tx_hash, blockchain_tx_id)) + { + cerr << "Tx " << oi_identification.tx_hash_str << "not found in blockchain !" << endl; + continue; + } + // FIRSt step. oi_identification.identify_outputs(); @@ -176,6 +187,7 @@ TxSearch::search() uint64_t tx_mysql_id {0}; + // if we identified some outputs as ours, // save them into mysql. if (!oi_identification.identified_outputs.empty()) @@ -195,27 +207,28 @@ TxSearch::search() << endl; } - tx_data.hash = oi_identification.tx_hash_str; - tx_data.prefix_hash = oi_identification.tx_prefix_hash_str; - tx_data.account_id = acc->id; - tx_data.total_received = oi_identification.total_received; - tx_data.total_sent = 0; // at this stage we don't have any - // info about spendings - - // this is current block + unlock time - // for regular tx, the unlock time is - // default of 10 blocks. - // for coinbase tx it is 60 blocks - tx_data.unlock_time = tx.unlock_time; - - tx_data.height = searched_blk_no; - tx_data.coinbase = oi_identification.tx_is_coinbase; - tx_data.is_rct = oi_identification.is_rct; - tx_data.rct_type = oi_identification.rct_type; - tx_data.spendable = is_spendable; - tx_data.payment_id = CurrentBlockchainStatus::get_payment_id_as_string(tx); - tx_data.mixin = oi_identification.mixin_no; - tx_data.timestamp = blk_timestamp_mysql_format; + tx_data.hash = oi_identification.tx_hash_str; + tx_data.prefix_hash = oi_identification.tx_prefix_hash_str; + tx_data.account_id = acc->id; + tx_data.blockchain_tx_id = blockchain_tx_id; + tx_data.total_received = oi_identification.total_received; + tx_data.total_sent = 0; // at this stage we don't have any + // info about spendings + + // this is current block + unlock time + // for regular tx, the unlock time is + // default of 10 blocks. + // for coinbase tx it is 60 blocks + tx_data.unlock_time = tx.unlock_time; + + tx_data.height = searched_blk_no; + tx_data.coinbase = oi_identification.tx_is_coinbase; + tx_data.is_rct = oi_identification.is_rct; + tx_data.rct_type = oi_identification.rct_type; + tx_data.spendable = is_spendable; + tx_data.payment_id = CurrentBlockchainStatus::get_payment_id_as_string(tx); + tx_data.mixin = oi_identification.mixin_no; + tx_data.timestamp = blk_timestamp_mysql_format; // insert tx_data into mysql's Transactions table @@ -343,20 +356,21 @@ TxSearch::search() XmrTransaction tx_data; - tx_data.hash = oi_identification.tx_hash_str; - tx_data.prefix_hash = oi_identification.tx_prefix_hash_str; - tx_data.account_id = acc->id; - tx_data.total_received = 0; // because this is spending, total_recieved is 0 - tx_data.total_sent = total_sent; - tx_data.unlock_time = tx.unlock_time; - tx_data.height = searched_blk_no; - tx_data.coinbase = oi_identification.tx_is_coinbase; - tx_data.is_rct = oi_identification.is_rct; - tx_data.rct_type = oi_identification.rct_type; - tx_data.spendable = is_spendable; - tx_data.payment_id = CurrentBlockchainStatus::get_payment_id_as_string(tx); - tx_data.mixin = get_mixin_no(tx) - 1; - tx_data.timestamp = blk_timestamp_mysql_format; + tx_data.hash = oi_identification.tx_hash_str; + tx_data.prefix_hash = oi_identification.tx_prefix_hash_str; + tx_data.account_id = acc->id; + tx_data.blockchain_tx_id = blockchain_tx_id; + tx_data.total_received = 0; // because this is spending, total_recieved is 0 + tx_data.total_sent = total_sent; + tx_data.unlock_time = tx.unlock_time; + tx_data.height = searched_blk_no; + tx_data.coinbase = oi_identification.tx_is_coinbase; + tx_data.is_rct = oi_identification.is_rct; + tx_data.rct_type = oi_identification.rct_type; + tx_data.spendable = is_spendable; + tx_data.payment_id = CurrentBlockchainStatus::get_payment_id_as_string(tx); + tx_data.mixin = get_mixin_no(tx) - 1; + tx_data.timestamp = blk_timestamp_mysql_format; // insert tx_data into mysql's Transactions table tx_mysql_id = xmr_accounts->insert_tx(tx_data); diff --git a/src/YourMoneroRequests.cpp b/src/YourMoneroRequests.cpp index 3af1df6..fefcaa8 100644 --- a/src/YourMoneroRequests.cpp +++ b/src/YourMoneroRequests.cpp @@ -190,7 +190,7 @@ YourMoneroRequests::get_address_txs(const shared_ptr< Session > session, const B for (XmrTransaction tx: txs) { json j_tx { - {"id" , tx.id}, // strangely, frontend sorts txs by id in database. + {"id" , tx.blockchain_tx_id}, {"coinbase" , bool {tx.coinbase}}, {"hash" , tx.hash}, {"height" , tx.height}, diff --git a/src/ssqlses.h b/src/ssqlses.h index 695ca27..137ec52 100644 --- a/src/ssqlses.h +++ b/src/ssqlses.h @@ -69,11 +69,12 @@ struct XmrAccount : public Accounts, Table }; -sql_create_15(Transactions, 1, 2, +sql_create_16(Transactions, 1, 2, sql_bigint_unsigned, id, sql_varchar , hash, sql_varchar , prefix_hash, sql_bigint_unsigned, account_id, + sql_bigint_unsigned, blockchain_tx_id, sql_bigint_unsigned, total_received, sql_bigint_unsigned, total_sent, sql_bigint_unsigned, unlock_time, @@ -107,16 +108,16 @@ struct XmrTransaction : public Transactions, Table )"; static constexpr const char* INSERT_STMT = R"( - INSERT IGNORE INTO `Transactions` (`hash`, `prefix_hash`, `account_id`, + INSERT IGNORE INTO `Transactions` (`hash`, `prefix_hash`, `account_id`, `blockchain_tx_id`, `total_received`, `total_sent`, `unlock_time`, `height`, `coinbase`, `is_rct`, `rct_type`, `spendable`, `payment_id`, `mixin`, `timestamp`) - VALUES (%0q, %1q, %2q, - %3q, %4q, %5q, - %6q, %7q, %8q, %9q, - %10q, - %11q, %12q, %13q); + VALUES (%0q, %1q, %2q, %3q, + %4q, %5q, %6q, + %7q, %8q, %9q, %10q, + %11q, + %12q, %13q, %14q); )"; static constexpr const char* MARK_AS_SPENDABLE_STMT = R"(