diff --git a/libraries/core_libs/consensus/include/transaction/transaction_manager.hpp b/libraries/core_libs/consensus/include/transaction/transaction_manager.hpp index 0d5f272f5d..ae24fb61a3 100644 --- a/libraries/core_libs/consensus/include/transaction/transaction_manager.hpp +++ b/libraries/core_libs/consensus/include/transaction/transaction_manager.hpp @@ -234,12 +234,17 @@ class TransactionManager : public std::enable_shared_from_this> nonfinalized_transactions_in_dag_; std::unordered_map> recently_finalized_transactions_; std::unordered_map> recently_finalized_transactions_per_period_; uint64_t trx_count_ = 0; + const uint64_t kGasEstimationCacheSize = 1000; + mutable ExpirationCacheMap> gas_estimation_cache_; + const uint64_t kDagBlockGasLimit; const uint64_t kEstimateGasLimit = 200000; const uint64_t kRecentlyFinalizedTransactionsMax = 50000; diff --git a/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp b/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp index 2ea8ef629a..69bdb144c8 100644 --- a/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp +++ b/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp @@ -14,6 +14,7 @@ TransactionManager::TransactionManager(FullNodeConfig const &conf, std::shared_p : kConf(conf), transactions_pool_(final_chain, kConf.transactions_pool_size), kDagBlockGasLimit(kConf.genesis.dag.gas_limit), + gas_estimation_cache_(kGasEstimationCacheSize, kGasEstimationCacheSize / 10), db_(std::move(db)), final_chain_(std::move(final_chain)) { LOG_OBJECTS_CREATE("TRXMGR"); @@ -28,6 +29,13 @@ uint64_t TransactionManager::estimateTransactionGas(std::shared_ptr if (trx->getGas() <= kEstimateGasLimit) { return trx->getGas(); } + + std::unique_lock transactions_lock(gas_estimations_mutex_); + + auto estimation = gas_estimation_cache_.get(trx->getHash()); + if (estimation.second && estimation.first.first == *proposal_period) { + return estimation.first.second; + } const auto &result = final_chain_->call( state_api::EVMTransaction{ trx->getSender(), @@ -43,6 +51,8 @@ uint64_t TransactionManager::estimateTransactionGas(std::shared_ptr if (!result.code_err.empty() || !result.consensus_err.empty()) { return 0; } + + gas_estimation_cache_.insert(trx->getHash(), {*proposal_period, result.gas_used}); return result.gas_used; }