From d17c0fc2d0ae52260a93ec8985b7ebb7ad3cfea7 Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Thu, 1 Jun 2017 13:29:51 +0100 Subject: Don't copy blockchain for coinbase_tx_sum Changed Blockchain::for_all_blocks() to for_blocks_range() Operate on blockchain in-place instead of building a copy first. --- src/blockchain_db/blockchain_db.h | 10 ++++++---- src/blockchain_db/lmdb/db_lmdb.cpp | 14 ++++++++++++-- src/blockchain_db/lmdb/db_lmdb.h | 2 +- src/cryptonote_core/blockchain.cpp | 4 ++-- src/cryptonote_core/blockchain.h | 6 ++++-- src/cryptonote_core/cryptonote_core.cpp | 9 ++++++--- tests/unit_tests/hardfork.cpp | 2 +- 7 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h index 9dd343f4b..b0a3d84e1 100644 --- a/src/blockchain_db/blockchain_db.h +++ b/src/blockchain_db/blockchain_db.h @@ -1353,10 +1353,10 @@ public: virtual bool for_all_key_images(std::function) const = 0; /** - * @brief runs a function over all blocks stored + * @brief runs a function over a range of blocks * - * The subclass should run the passed function for each block it has - * stored, passing (block_height, block_hash, block) as its parameters. + * The subclass should run the passed function for each block in the + * specified range, passing (block_height, block_hash, block) as its parameters. * * If any call to the function returns false, the subclass should return * false. Otherwise, the subclass returns true. @@ -1364,11 +1364,13 @@ public: * The subclass should throw DB_ERROR if any of the expected values are * not found. Current implementations simply return false. * + * @param h1 the start height + * @param h2 the end height * @param std::function fn the function to run * * @return false if the function returns false for any block, otherwise true */ - virtual bool for_all_blocks(std::function) const = 0; + virtual bool for_blocks_range(const uint64_t& h1, const uint64_t& h2, std::function) const = 0; /** * @brief runs a function over all transactions stored diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index 1f0831874..d7947c8d0 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -2380,7 +2380,7 @@ bool BlockchainLMDB::for_all_key_images(std::function f) const +bool BlockchainLMDB::for_blocks_range(const uint64_t& h1, const uint64_t& h2, std::function f) const { LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); @@ -2392,7 +2392,15 @@ bool BlockchainLMDB::for_all_blocks(std::function= h2) + break; } TXN_POSTFIX_RDONLY(); diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h index 02f57ce18..540fababb 100644 --- a/src/blockchain_db/lmdb/db_lmdb.h +++ b/src/blockchain_db/lmdb/db_lmdb.h @@ -249,7 +249,7 @@ public: virtual bool for_all_txpool_txes(std::function f, bool include_blob = false) const; virtual bool for_all_key_images(std::function) const; - virtual bool for_all_blocks(std::function) const; + virtual bool for_blocks_range(const uint64_t& h1, const uint64_t& h2, std::function) const; virtual bool for_all_transactions(std::function) const; virtual bool for_all_outputs(std::function f) const; diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 7fb85ee76..6f2977c5b 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -4164,9 +4164,9 @@ bool Blockchain::for_all_key_images(std::functionfor_all_key_images(f); } -bool Blockchain::for_all_blocks(std::function f) const +bool Blockchain::for_blocks_range(const uint64_t& h1, const uint64_t& h2, std::function f) const { - return m_db->for_all_blocks(f); + return m_db->for_blocks_range(h1, h2, f); } bool Blockchain::for_all_transactions(std::function f) const diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index 89efe5452..52172012c 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -790,13 +790,15 @@ namespace cryptonote bool for_all_key_images(std::function) const; /** - * @brief perform a check on all blocks in the blockchain + * @brief perform a check on all blocks in the blockchain in the given range * + * @param h1 the start height + * @param h2 the end height * @param std::function the check to perform, pass/fail * * @return false if any block fails the check, otherwise true */ - bool for_all_blocks(std::function) const; + bool for_blocks_range(const uint64_t& h1, const uint64_t& h2, std::function) const; /** * @brief perform a check on all transactions in the blockchain diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index 21b0b4ceb..ff8bbef64 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -713,12 +713,13 @@ namespace cryptonote //----------------------------------------------------------------------------------------------- std::pair core::get_coinbase_tx_sum(const uint64_t start_offset, const size_t count) { - std::list blocks; uint64_t emission_amount = 0; uint64_t total_fee_amount = 0; - this->get_blocks(start_offset, count, blocks); - for(auto& b: blocks) + if (count) { + const uint64_t end = start_offset + count - 1; + m_blockchain_storage.for_blocks_range(start_offset, end, + [this, &emission_amount, &total_fee_amount](uint64_t, const crypto::hash& hash, const block& b){ std::list txs; std::list missed_txs; uint64_t coinbase_amount = get_outs_money_amount(b.miner_tx); @@ -731,6 +732,8 @@ namespace cryptonote emission_amount += coinbase_amount - tx_fee_amount; total_fee_amount += tx_fee_amount; + return true; + }); } return std::pair(emission_amount, total_fee_amount); diff --git a/tests/unit_tests/hardfork.cpp b/tests/unit_tests/hardfork.cpp index e29919e8f..4bfe90733 100644 --- a/tests/unit_tests/hardfork.cpp +++ b/tests/unit_tests/hardfork.cpp @@ -106,7 +106,7 @@ public: virtual void remove_spent_key(const crypto::key_image& k_image) {} virtual bool for_all_key_images(std::function) const { return true; } - virtual bool for_all_blocks(std::function) const { return true; } + virtual bool for_blocks_range(const uint64_t&, const uint64_t&, std::function) const { return true; } virtual bool for_all_transactions(std::function) const { return true; } virtual bool for_all_outputs(std::function f) const { return true; } virtual bool is_read_only() const { return false; } -- cgit v1.2.3