Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas estimation cache #2783

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/common/include/common/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class ExpirationCacheMap {
bool insert(Key const &key, Value const &value) {
{
std::shared_lock lock(mtx_);
if (cache_.count(key)) {
if (cache_.contains(key)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,18 @@ class TransactionManager : public std::enable_shared_from_this<TransactionManage
// Transactions can be in one of three states:
// 1. In transactions pool; 2. In non-finalized Dag block 3. Executed
mutable std::shared_mutex transactions_mutex_;
mutable std::mutex gas_estimations_mutex_;

TransactionQueue transactions_pool_;
std::unordered_map<trx_hash_t, std::shared_ptr<Transaction>> nonfinalized_transactions_in_dag_;
std::unordered_map<trx_hash_t, std::shared_ptr<Transaction>> recently_finalized_transactions_;
std::unordered_map<PbftPeriod, std::vector<trx_hash_t>> recently_finalized_transactions_per_period_;
uint64_t trx_count_ = 0;

const uint64_t kGasEstimationCacheSize = 1000;
mutable ExpirationCacheMap<trx_hash_t, std::pair<PbftPeriod, uint64_t>> gas_estimation_cache_;
mutable std::unordered_map<trx_hash_t, std::shared_ptr<std::condition_variable>> gas_estimation_condition_vars_;

const uint64_t kDagBlockGasLimit;
const uint64_t kEstimateGasLimit = 200000;
const uint64_t kRecentlyFinalizedTransactionsMax = 50000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -28,6 +29,27 @@ uint64_t TransactionManager::estimateTransactionGas(std::shared_ptr<Transaction>
if (trx->getGas() <= kEstimateGasLimit) {
return trx->getGas();
}

const auto trx_hash = trx->getHash();
std::unique_lock transactions_lock(gas_estimations_mutex_);

const auto [estimation, exist] = gas_estimation_cache_.get(trx_hash);

if (!exist) {
gas_estimation_cache_.insert(trx_hash, {*proposal_period, -1});
gas_estimation_condition_vars_[trx_hash] = std::make_shared<std::condition_variable>();
} else {
if (estimation.first == *proposal_period && estimation.second != -1) {
return estimation.second;
}
auto cond_var = gas_estimation_condition_vars_[trx_hash];
cond_var->wait(transactions_lock, []{ return true; });
gas_estimation_condition_vars_.erase(trx_hash);
return gas_estimation_cache_.get(trx_hash).first.second;
}

transactions_lock.unlock();

const auto &result = final_chain_->call(
state_api::EVMTransaction{
trx->getSender(),
Expand All @@ -40,9 +62,16 @@ uint64_t TransactionManager::estimateTransactionGas(std::shared_ptr<Transaction>
},
proposal_period);

transactions_lock.lock();

if (!result.code_err.empty() || !result.consensus_err.empty()) {
gas_estimation_cache_.insert(trx_hash, {*proposal_period, 0});
gas_estimation_condition_vars_[trx_hash]->notify_all();
return 0;
}

gas_estimation_cache_.insert(trx_hash, {*proposal_period, result.gas_used});
gas_estimation_condition_vars_[trx_hash]->notify_all();
return result.gas_used;
}

Expand Down
Loading