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

chore: transactions gas estimation cache #2780

Closed
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,17 @@ 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::shared_mutex gas_estimations_mutex_;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this ExpirationCacheMap is threadsafe


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_;

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,13 @@ uint64_t TransactionManager::estimateTransactionGas(std::shared_ptr<Transaction>
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(),
Expand All @@ -43,6 +51,8 @@ uint64_t TransactionManager::estimateTransactionGas(std::shared_ptr<Transaction>
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;
}

Expand Down
Loading