diff options
author | Riccardo Spagni <ric@spagni.net> | 2017-01-08 16:33:41 -0800 |
---|---|---|
committer | Riccardo Spagni <ric@spagni.net> | 2017-01-08 16:33:41 -0800 |
commit | 23cf9633325693b363a608f3021270231640d848 (patch) | |
tree | b8c45ab85881215e43376e0935e74fe18bd67ec8 | |
parent | Merge pull request #1491 (diff) | |
parent | blockchain: allow marking "tx not found" without an exception (diff) | |
download | monero-23cf9633325693b363a608f3021270231640d848.tar.xz |
Merge pull request #1493
0478ac68 blockchain: allow marking "tx not found" without an exception (moneromooo-monero)
-rw-r--r-- | src/blockchain_db/blockchain_db.h | 14 | ||||
-rw-r--r-- | src/blockchain_db/lmdb/db_lmdb.cpp | 14 | ||||
-rw-r--r-- | src/blockchain_db/lmdb/db_lmdb.h | 2 | ||||
-rw-r--r-- | src/cryptonote_core/blockchain.cpp | 10 | ||||
-rw-r--r-- | tests/unit_tests/hardfork.cpp | 1 |
5 files changed, 33 insertions, 8 deletions
diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h index 91c388de6..b39cb1801 100644 --- a/src/blockchain_db/blockchain_db.h +++ b/src/blockchain_db/blockchain_db.h @@ -1020,6 +1020,20 @@ public: virtual transaction get_tx(const crypto::hash& h) const = 0; /** + * @brief fetches the transaction with the given hash + * + * The subclass should return the transaction stored which has the given + * hash. + * + * If the transaction does not exist, the subclass should return false. + * + * @param h the hash to look for + * + * @return true iff the transaction was found + */ + virtual bool get_tx(const crypto::hash& h, transaction &tx) const = 0; + + /** * @brief fetches the total number of transactions ever * * The subclass should return a count of all the transactions from diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index 1ad9876ac..ba2cb60bd 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -1791,7 +1791,7 @@ uint64_t BlockchainLMDB::get_tx_unlock_time(const crypto::hash& h) const return ret; } -transaction BlockchainLMDB::get_tx(const crypto::hash& h) const +bool BlockchainLMDB::get_tx(const crypto::hash& h, transaction &tx) const { LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); @@ -1810,19 +1810,27 @@ transaction BlockchainLMDB::get_tx(const crypto::hash& h) const get_result = mdb_cursor_get(m_cur_txs, &val_tx_id, &result, MDB_SET); } if (get_result == MDB_NOTFOUND) - throw1(TX_DNE(std::string("tx with hash ").append(epee::string_tools::pod_to_hex(h)).append(" not found in db").c_str())); + return false; else if (get_result) throw0(DB_ERROR(lmdb_error("DB error attempting to fetch tx from hash", get_result).c_str())); blobdata bd; bd.assign(reinterpret_cast<char*>(result.mv_data), result.mv_size); - transaction tx; if (!parse_and_validate_tx_from_blob(bd, tx)) throw0(DB_ERROR("Failed to parse tx from blob retrieved from the db")); TXN_POSTFIX_RDONLY(); + return true; +} + +transaction BlockchainLMDB::get_tx(const crypto::hash& h) const +{ + transaction tx; + + if (!get_tx(h, tx)) + throw1(TX_DNE(std::string("tx with hash ").append(epee::string_tools::pod_to_hex(h)).append(" not found in db").c_str())); return tx; } diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h index 6db5abca1..b0d8d9d0a 100644 --- a/src/blockchain_db/lmdb/db_lmdb.h +++ b/src/blockchain_db/lmdb/db_lmdb.h @@ -209,6 +209,8 @@ public: virtual transaction get_tx(const crypto::hash& h) const; + virtual bool get_tx(const crypto::hash& h, transaction &tx) const; + virtual uint64_t get_tx_count() const; virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const; diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 42279184a..39fd0f6f1 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -1895,11 +1895,11 @@ bool Blockchain::get_transactions(const t_ids_container& txs_ids, t_tx_container { try { - txs.push_back(m_db->get_tx(tx_hash)); - } - catch (const TX_DNE& e) - { - missed_txs.push_back(tx_hash); + transaction tx; + if (m_db->get_tx(tx_hash, tx)) + txs.push_back(std::move(tx)); + else + missed_txs.push_back(tx_hash); } catch (const std::exception& e) { diff --git a/tests/unit_tests/hardfork.cpp b/tests/unit_tests/hardfork.cpp index be2885f45..ca72f5e0e 100644 --- a/tests/unit_tests/hardfork.cpp +++ b/tests/unit_tests/hardfork.cpp @@ -78,6 +78,7 @@ public: virtual bool tx_exists(const crypto::hash& h, uint64_t& tx_index) const { return false; } virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const { return 0; } virtual transaction get_tx(const crypto::hash& h) const { return transaction(); } + virtual bool get_tx(const crypto::hash& h, transaction &tx) const { return false; } virtual uint64_t get_tx_count() const { return 0; } virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const { return std::vector<transaction>(); } virtual uint64_t get_tx_block_height(const crypto::hash& h) const { return 0; } |