From 4d25350a823866a602dca84847f7d7e3b810c823 Mon Sep 17 00:00:00 2001 From: Zachary Michaels Date: Thu, 17 Jul 2014 10:31:44 -0400 Subject: [PATCH] Use const where appropriate in tx_pool --- src/cryptonote_core/cryptonote_core.cpp | 3 +- src/cryptonote_core/tx_pool.cpp | 61 ++++++++++--------------- src/cryptonote_core/tx_pool.h | 22 ++++----- 3 files changed, 36 insertions(+), 50 deletions(-) diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index 2a0129f4e..751d12f79 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -452,7 +452,8 @@ namespace cryptonote //----------------------------------------------------------------------------------------------- bool core::get_pool_transactions(std::list& txs) { - return m_mempool.get_transactions(txs); + m_mempool.get_transactions(txs); + return true; } //----------------------------------------------------------------------------------------------- bool core::get_short_chain_history(std::list& ids) diff --git a/src/cryptonote_core/tx_pool.cpp b/src/cryptonote_core/tx_pool.cpp index bf932404a..11e899c1c 100644 --- a/src/cryptonote_core/tx_pool.cpp +++ b/src/cryptonote_core/tx_pool.cpp @@ -204,22 +204,20 @@ namespace cryptonote return true; } //--------------------------------------------------------------------------------- - size_t tx_memory_pool::get_transactions_count() + size_t tx_memory_pool::get_transactions_count() const { CRITICAL_REGION_LOCAL(m_transactions_lock); return m_transactions.size(); } //--------------------------------------------------------------------------------- - bool tx_memory_pool::get_transactions(std::list& txs) + void tx_memory_pool::get_transactions(std::list& txs) const { CRITICAL_REGION_LOCAL(m_transactions_lock); BOOST_FOREACH(const auto& tx_vt, m_transactions) txs.push_back(tx_vt.second.tx); - - return true; } //--------------------------------------------------------------------------------- - bool tx_memory_pool::get_transaction(const crypto::hash& id, transaction& tx) + bool tx_memory_pool::get_transaction(const crypto::hash& id, transaction& tx) const { CRITICAL_REGION_LOCAL(m_transactions_lock); auto it = m_transactions.find(id); @@ -239,7 +237,7 @@ namespace cryptonote return true; } //--------------------------------------------------------------------------------- - bool tx_memory_pool::have_tx(const crypto::hash &id) + bool tx_memory_pool::have_tx(const crypto::hash &id) const { CRITICAL_REGION_LOCAL(m_transactions_lock); if(m_transactions.count(id)) @@ -247,7 +245,7 @@ namespace cryptonote return false; } //--------------------------------------------------------------------------------- - bool tx_memory_pool::have_tx_keyimges_as_spent(const transaction& tx) + bool tx_memory_pool::have_tx_keyimges_as_spent(const transaction& tx) const { CRITICAL_REGION_LOCAL(m_transactions_lock); BOOST_FOREACH(const auto& in, tx.vin) @@ -259,23 +257,23 @@ namespace cryptonote return false; } //--------------------------------------------------------------------------------- - bool tx_memory_pool::have_tx_keyimg_as_spent(const crypto::key_image& key_im) + bool tx_memory_pool::have_tx_keyimg_as_spent(const crypto::key_image& key_im) const { CRITICAL_REGION_LOCAL(m_transactions_lock); return m_spent_key_images.end() != m_spent_key_images.find(key_im); } //--------------------------------------------------------------------------------- - void tx_memory_pool::lock() + void tx_memory_pool::lock() const { m_transactions_lock.lock(); } //--------------------------------------------------------------------------------- - void tx_memory_pool::unlock() + void tx_memory_pool::unlock() const { m_transactions_lock.unlock(); } //--------------------------------------------------------------------------------- - bool tx_memory_pool::is_transaction_ready_to_go(tx_details& txd) + bool tx_memory_pool::is_transaction_ready_to_go(tx_details& txd) const { //not the best implementation at this time, sorry :( //check is ring_signature already checked ? @@ -339,38 +337,25 @@ namespace cryptonote return true; } //--------------------------------------------------------------------------------- - std::string tx_memory_pool::print_pool(bool short_format) + std::string tx_memory_pool::print_pool(bool short_format) const { std::stringstream ss; CRITICAL_REGION_LOCAL(m_transactions_lock); - BOOST_FOREACH(transactions_container::value_type& txe, m_transactions) - { - if(short_format) - { - tx_details& txd = txe.second; - ss << "id: " << txe.first << ENDL - << "blob_size: " << txd.blob_size << ENDL - << "fee: " << txd.fee << ENDL - << "kept_by_block: " << (txd.kept_by_block ? "true":"false") << ENDL - << "max_used_block_height: " << txd.max_used_block_height << ENDL - << "max_used_block_id: " << txd.max_used_block_id << ENDL - << "last_failed_height: " << txd.last_failed_height << ENDL - << "last_failed_id: " << txd.last_failed_id << ENDL; - }else - { - tx_details& txd = txe.second; - ss << "id: " << txe.first << ENDL - << obj_to_json_str(txd.tx) << ENDL - << "blob_size: " << txd.blob_size << ENDL - << "fee: " << txd.fee << ENDL - << "kept_by_block: " << (txd.kept_by_block ? "true":"false") << ENDL - << "max_used_block_height: " << txd.max_used_block_height << ENDL - << "max_used_block_id: " << txd.max_used_block_id << ENDL - << "last_failed_height: " << txd.last_failed_height << ENDL - << "last_failed_id: " << txd.last_failed_id << ENDL; + for (const transactions_container::value_type& txe : m_transactions) { + const tx_details& txd = txe.second; + ss << "id: " << txe.first << std::endl; + if (!short_format) { + ss << obj_to_json_str(*const_cast(&txd.tx)) << std::endl; } - + ss << "blob_size: " << txd.blob_size << std::endl + << "fee: " << print_money(txd.fee) << std::endl + << "kept_by_block: " << (txd.kept_by_block ? 'T' : 'F') << std::endl + << "max_used_block_height: " << txd.max_used_block_height << std::endl + << "max_used_block_id: " << txd.max_used_block_id << std::endl + << "last_failed_height: " << txd.last_failed_height << std::endl + << "last_failed_id: " << txd.last_failed_id << std::endl; } + return ss.str(); } //--------------------------------------------------------------------------------- diff --git a/src/cryptonote_core/tx_pool.h b/src/cryptonote_core/tx_pool.h index 6bb6189ec..91fc18b5b 100644 --- a/src/cryptonote_core/tx_pool.h +++ b/src/cryptonote_core/tx_pool.h @@ -35,22 +35,22 @@ namespace cryptonote //gets tx and remove it from pool bool take_tx(const crypto::hash &id, transaction &tx, size_t& blob_size, uint64_t& fee); - bool have_tx(const crypto::hash &id); + bool have_tx(const crypto::hash &id) const; bool on_blockchain_inc(uint64_t new_block_height, const crypto::hash& top_block_id); bool on_blockchain_dec(uint64_t new_block_height, const crypto::hash& top_block_id); void on_idle(); - void lock(); - void unlock(); + void lock() const; + void unlock() const; // load/store operations bool init(const std::string& config_folder); bool deinit(); bool fill_block_template(block &bl, size_t median_size, uint64_t already_generated_coins, size_t &total_size, uint64_t &fee); - bool get_transactions(std::list& txs); - bool get_transaction(const crypto::hash& h, transaction& tx); - size_t get_transactions_count(); - std::string print_pool(bool short_format); + void get_transactions(std::list& txs) const; + bool get_transaction(const crypto::hash& h, transaction& tx) const; + size_t get_transactions_count() const; + std::string print_pool(bool short_format) const; /*bool flush_pool(const std::strig& folder); bool inflate_pool(const std::strig& folder);*/ @@ -83,17 +83,17 @@ namespace cryptonote private: bool remove_stuck_transactions(); - bool have_tx_keyimg_as_spent(const crypto::key_image& key_im); - bool have_tx_keyimges_as_spent(const transaction& tx); + bool have_tx_keyimg_as_spent(const crypto::key_image& key_im) const; + bool have_tx_keyimges_as_spent(const transaction& tx) const; bool remove_transaction_keyimages(const transaction& tx); static bool have_key_images(const std::unordered_set& kic, const transaction& tx); static bool append_key_images(std::unordered_set& kic, const transaction& tx); - bool is_transaction_ready_to_go(tx_details& txd); + bool is_transaction_ready_to_go(tx_details& txd) const; typedef std::unordered_map transactions_container; typedef std::unordered_map > key_images_container; - epee::critical_section m_transactions_lock; + mutable epee::critical_section m_transactions_lock; transactions_container m_transactions; key_images_container m_spent_key_images; epee::math_helper::once_a_time_seconds<30> m_remove_stuck_tx_interval;