From d1e6ef429bf8568d09baa54a40f4b953b4b987de Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Fri, 15 Nov 2019 01:04:51 +0100 Subject: [PATCH] refactoring of tx pool/core block's transactions handling --- src/currency_core/blockchain_storage.cpp | 2 +- .../currency_format_utils_transactions.cpp | 10 ++++++++++ .../currency_format_utils_transactions.h | 1 + src/currency_core/tx_pool.cpp | 17 +++++++++++++---- src/currency_core/tx_pool.h | 2 +- src/currency_core/verification_context.h | 4 ++++ 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index eaa25474..bd72ce8c 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -1351,7 +1351,7 @@ bool blockchain_storage::create_block_template(const create_block_template_param uint64_t fee; bool block_filled = false; if (pcustom_fill_block_template_func == nullptr) - block_filled = m_tx_pool.fill_block_template(b, pos, median_size, already_generated_coins, txs_size, fee, height); + block_filled = m_tx_pool.fill_block_template(b, pos, median_size, already_generated_coins, txs_size, fee, height, params.explicit_txs); else block_filled = (*pcustom_fill_block_template_func)(b, pos, median_size, already_generated_coins, txs_size, fee, height); diff --git a/src/currency_core/currency_format_utils_transactions.cpp b/src/currency_core/currency_format_utils_transactions.cpp index ba08914b..20ecd4e0 100644 --- a/src/currency_core/currency_format_utils_transactions.cpp +++ b/src/currency_core/currency_format_utils_transactions.cpp @@ -193,6 +193,16 @@ namespace currency return get_object_blobsize(t, tx_blob_size); } //--------------------------------------------------------------- + size_t get_objects_blobsize(const std::list& ls) + { + size_t total = 0; + for (const auto& tx : ls) + { + total += get_object_blobsize(tx); + } + return total; + } + //--------------------------------------------------------------- size_t get_object_blobsize(const transaction& t, uint64_t prefix_blob_size) { size_t tx_blob_size = prefix_blob_size; diff --git a/src/currency_core/currency_format_utils_transactions.h b/src/currency_core/currency_format_utils_transactions.h index 4b993394..626fc156 100644 --- a/src/currency_core/currency_format_utils_transactions.h +++ b/src/currency_core/currency_format_utils_transactions.h @@ -104,6 +104,7 @@ namespace currency bool get_transaction_hash(const transaction& t, crypto::hash& res); bool get_transaction_hash(const transaction& t, crypto::hash& res, uint64_t& blob_size); size_t get_object_blobsize(const transaction& t); + size_t get_objects_blobsize(const std::list& ls); size_t get_object_blobsize(const transaction& t, uint64_t prefix_blob_size); blobdata tx_to_blob(const transaction& b); bool tx_to_blob(const transaction& b, blobdata& b_blob); diff --git a/src/currency_core/tx_pool.cpp b/src/currency_core/tx_pool.cpp index ae8737a8..aef91d5a 100644 --- a/src/currency_core/tx_pool.cpp +++ b/src/currency_core/tx_pool.cpp @@ -987,7 +987,9 @@ namespace currency const boost::multiprecision::uint128_t& already_generated_coins, size_t &total_size, uint64_t &fee, - uint64_t height) + uint64_t height, + const std::list& explicit_txs + ) { LOCAL_READONLY_TRANSACTION(); //typedef transactions_container::value_type txv; @@ -998,8 +1000,8 @@ namespace currency txs_v.reserve(m_db_transactions.size()); std::vector txs; - //std::transform(m_transactions.begin(), m_transactions.end(), txs.begin(), [](txv &a) -> txv * { return &a; }); - //keep getting it as a values cz db items cache will keep it as unserialied object stored by shared ptrs + + //keep getting it as a values cz db items cache will keep it as unserialised object stored by shared ptrs m_db_transactions.enumerate_keys([&](uint64_t i, crypto::hash& k){txs_v.resize(i + 1); txs_v[i].first = k; return true;}); txs.resize(txs_v.size(), nullptr); @@ -1024,7 +1026,9 @@ namespace currency return a_ > b_; }); - size_t current_size = 0; + + size_t explicit_total_size = get_objects_blobsize(explicit_txs); + size_t current_size = explicit_total_size; uint64_t current_fee = 0; uint64_t best_money; if (!get_block_reward(pos, median_size, CURRENCY_COINBASE_BLOB_RESERVED_SIZE, already_generated_coins, best_money, height)) { @@ -1133,6 +1137,11 @@ namespace currency } } } + // add explicit transactions + for (const auto& tx : explicit_txs) + { + bl.tx_hashes.push_back(get_transaction_hash(tx)); + } return true; } //--------------------------------------------------------------------------------- diff --git a/src/currency_core/tx_pool.h b/src/currency_core/tx_pool.h index 6d9bad9d..d8642e8e 100644 --- a/src/currency_core/tx_pool.h +++ b/src/currency_core/tx_pool.h @@ -117,7 +117,7 @@ namespace currency // load/store operations bool init(const std::string& config_folder, const boost::program_options::variables_map& vm); bool deinit(); - bool fill_block_template(block &bl, bool pos, size_t median_size, const boost::multiprecision::uint128_t& already_generated_coins, size_t &total_size, uint64_t &fee, uint64_t height); + bool fill_block_template(block &bl, bool pos, size_t median_size, const boost::multiprecision::uint128_t& already_generated_coins, size_t &total_size, uint64_t &fee, uint64_t height, const std::list& explicit_txs); bool get_transactions(std::list& txs) const; bool get_all_transactions_details(std::list& txs)const; bool get_all_transactions_brief_details(std::list& txs)const; diff --git a/src/currency_core/verification_context.h b/src/currency_core/verification_context.h index a9f1f646..f952e3bd 100644 --- a/src/currency_core/verification_context.h +++ b/src/currency_core/verification_context.h @@ -27,5 +27,9 @@ namespace currency bool m_already_exists; bool added_to_altchain; uint64_t height_difference; + //this is work like a first-level cache for transactions while block is getting handled. It lets transactions + //associated with the block to get handled directly to core without being handled by tx_pool(which makes full + //inputs validation, including signatures check) + std::unordered_map m_onboard_transactions; }; }