Skip to content

Commit

Permalink
chore: gas estimation cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrankovi committed Jun 12, 2024
1 parent 1b10f9f commit 3ad9a61
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
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_;

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

0 comments on commit 3ad9a61

Please sign in to comment.