Skip to content

Commit 4160b96

Browse files
committed
chore: improve eth_getLogs
1 parent 9093f0b commit 4160b96

File tree

5 files changed

+47
-19
lines changed

5 files changed

+47
-19
lines changed

libraries/core_libs/consensus/include/final_chain/cache.hpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,19 @@ class ValueByBlockCache {
121121
}
122122
}
123123

124+
std::optional<Value> getFromCache(uint64_t block_num) const {
125+
std::shared_lock lock(mutex_);
126+
auto blk_entry = data_by_block_.find(block_num);
127+
if (blk_entry != data_by_block_.end()) {
128+
return blk_entry->second;
129+
}
130+
return {};
131+
}
132+
124133
Value get(uint64_t block_num) const {
125-
{
126-
std::shared_lock lock(mutex_);
127-
auto blk_entry = data_by_block_.find(block_num);
128-
if (blk_entry != data_by_block_.end()) {
129-
return blk_entry->second;
130-
}
134+
auto blk_entry = getFromCache(block_num);
135+
if (blk_entry) {
136+
return *blk_entry;
131137
}
132138

133139
auto value = getter_fn_(block_num);

libraries/core_libs/consensus/include/final_chain/final_chain.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,15 @@ class FinalChain {
131131
std::optional<TransactionReceipt> transactionReceipt(EthBlockNumber blk_n, uint64_t position,
132132
std::optional<trx_hash_t> trx_hash = {}) const;
133133

134-
/**
134+
std::shared_ptr<Transaction> transaction(EthBlockNumber blk_n, uint32_t position) const {
135+
auto blk_trxs = transactions_cache_.getFromCache(blk_n);
136+
if(blk_trxs) {
137+
return (*blk_trxs)[position];
138+
}
139+
return db_->getTransaction(blk_n, position);
140+
}
141+
142+
/**
135143
* @brief Method to get transactions count in block
136144
* @param n block number
137145
* @return count of transactions in block

libraries/core_libs/network/rpc/eth/LogFilter.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,26 @@ void LogFilter::match_one(const ExtendedTransactionLocation& trx_loc, const Tran
126126

127127
std::vector<LocalisedLogEntry> LogFilter::match_all(const final_chain::FinalChain& final_chain) const {
128128
std::vector<LocalisedLogEntry> ret;
129-
130129
auto action = [&, this](EthBlockNumber blk_n) {
131130
ExtendedTransactionLocation trx_loc{{{blk_n}, *final_chain.blockHash(blk_n)}};
132-
auto hashes = final_chain.transactionHashes(trx_loc.period);
133131
auto block_receipts = final_chain.blockReceipts(blk_n);
134-
135-
if (block_receipts && block_receipts->size() == hashes->size()) {
132+
if (block_receipts && block_receipts->size()) {
133+
std::vector<std::pair<uint64_t, LocalisedLogEntry>> ret_block;
136134
for (uint32_t i = 0; i < block_receipts->size(); i++) {
137-
trx_loc.trx_hash = (*hashes)[i];
138-
match_one(trx_loc, (*block_receipts)[i], [&](const auto& lle) { ret.push_back(lle); });
135+
match_one(trx_loc, (*block_receipts)[i], [&](const auto& lle) { ret_block.push_back({i, lle}); });
139136
++trx_loc.position;
140137
}
138+
if (ret_block.size() > 0) {
139+
for (auto& r : ret_block) {
140+
auto transaction = final_chain.transaction(trx_loc.period, r.first);
141+
if(transaction) {
142+
r.second.trx_loc.trx_hash = transaction->getHash();
143+
ret.push_back(r.second);
144+
}
145+
}
146+
}
141147
} else {
148+
auto hashes = final_chain.transactionHashes(trx_loc.period);
142149
for (const auto& hash : *hashes) {
143150
trx_loc.trx_hash = hash;
144151
match_one(trx_loc, *final_chain.transactionReceipt(trx_loc.period, trx_loc.position, hash),

libraries/core_libs/storage/include/storage/storage.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ class DbStorage : public std::enable_shared_from_this<DbStorage> {
261261

262262
// Transaction
263263
std::shared_ptr<Transaction> getTransaction(trx_hash_t const& hash) const;
264+
std::shared_ptr<Transaction> getTransaction(PbftPeriod period, uint32_t position) const;
265+
264266
SharedTransactions getAllNonfinalizedTransactions();
265267
bool transactionInDb(trx_hash_t const& hash);
266268
bool transactionFinalized(trx_hash_t const& hash);

libraries/core_libs/storage/src/storage.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -850,19 +850,24 @@ std::shared_ptr<Transaction> DbStorage::getTransaction(trx_hash_t const& hash) c
850850
}
851851
auto location = getTransactionLocation(hash);
852852
if (location && !location->is_system) {
853-
auto period_data = getPeriodDataRaw(location->period);
854-
if (period_data.size() > 0) {
855-
auto period_data_rlp = dev::RLP(period_data);
856-
auto transaction_data = period_data_rlp[TRANSACTIONS_POS_IN_PERIOD_DATA];
857-
return std::make_shared<Transaction>(transaction_data[location->position]);
858-
}
853+
return getTransaction(location->period, location->position);
859854
} else {
860855
// get system trx from a different column
861856
return getSystemTransaction(hash);
862857
}
863858
return nullptr;
864859
}
865860

861+
std::shared_ptr<Transaction> DbStorage::getTransaction(PbftPeriod period, uint32_t position) const {
862+
auto period_data = getPeriodDataRaw(period);
863+
if (period_data.size() > 0) {
864+
auto period_data_rlp = dev::RLP(period_data);
865+
auto transaction_data = period_data_rlp[TRANSACTIONS_POS_IN_PERIOD_DATA];
866+
return std::make_shared<Transaction>(transaction_data[position]);
867+
}
868+
return nullptr;
869+
}
870+
866871
uint64_t DbStorage::getTransactionCount(PbftPeriod period) const {
867872
auto period_data = getPeriodDataRaw(period);
868873
if (period_data.size()) {

0 commit comments

Comments
 (0)