From a1a08074ba8d70402ce126a511b4df52232dc110 Mon Sep 17 00:00:00 2001 From: Sixtysixter <20945591+Sixtysixter@users.noreply.github.com> Date: Thu, 12 Dec 2024 14:37:10 +0100 Subject: [PATCH] rpcdaemon: fix and improve perf in eth_getLogs (#2584) --- .github/workflows/rpc-integration-tests.yml | 2 +- .github/workflows/run_integration_tests.sh | 3 +-- silkworm/db/kv/txn_num.cpp | 12 +++++------- silkworm/db/kv/txn_num.hpp | 1 - silkworm/rpc/core/logs_walker.cpp | 14 +++++--------- 5 files changed, 12 insertions(+), 20 deletions(-) diff --git a/.github/workflows/rpc-integration-tests.yml b/.github/workflows/rpc-integration-tests.yml index ea4e6e3e67..328d4e4590 100644 --- a/.github/workflows/rpc-integration-tests.yml +++ b/.github/workflows/rpc-integration-tests.yml @@ -33,7 +33,7 @@ jobs: - name: Checkout RPC Tests Repository & Install Requirements run: | rm -rf ${{runner.workspace}}/rpc-tests - git -c advice.detachedHead=false clone --depth 1 --branch v1.24.0 https://github.com/erigontech/rpc-tests ${{runner.workspace}}/rpc-tests + git -c advice.detachedHead=false clone --depth 1 --branch v1.25.0 https://github.com/erigontech/rpc-tests ${{runner.workspace}}/rpc-tests cd ${{runner.workspace}}/rpc-tests pip3 install -r requirements.txt --break-system-packages diff --git a/.github/workflows/run_integration_tests.sh b/.github/workflows/run_integration_tests.sh index e42a7ba8e5..8d06af72be 100755 --- a/.github/workflows/run_integration_tests.sh +++ b/.github/workflows/run_integration_tests.sh @@ -43,8 +43,7 @@ debug_traceTransaction/test_92.tar,\ debug_traceTransaction/test_96.json,\ trace_replayBlockTransactions/test_29,\ engine_,\ -erigon_getLatestLogs,\ -eth_getLogs +erigon_getLatestLogs failed_test=$? diff --git a/silkworm/db/kv/txn_num.cpp b/silkworm/db/kv/txn_num.cpp index dee39d2830..0e1a07fcca 100644 --- a/silkworm/db/kv/txn_num.cpp +++ b/silkworm/db/kv/txn_num.cpp @@ -150,20 +150,18 @@ Task> PaginatedTransactionInfoIterator::next() { } const auto tnx_id = static_cast(next_id.value()); - if (min_txn_num_ == 0 || (ascending_ && next_id > max_txn_num_) || (!ascending_ && next_id < min_txn_num_)) { + bool block_changed{false}; + if (max_txn_num_ == 0 || (ascending_ && tnx_id > max_txn_num_) || (!ascending_ && tnx_id < min_txn_num_)) { const auto block_num_opt = co_await db::txn::block_num_from_tx_num(tx_, tnx_id, provider_); if (!block_num_opt) { SILK_DEBUG << "No block found for txn_id " << tnx_id; co_return std::nullopt; } block_num_ = block_num_opt.value(); - block_changed_ = true; - } - - if (block_changed_) { max_txn_num_ = co_await db::txn::max_tx_num(tx_, block_num_, provider_); min_txn_num_ = co_await db::txn::min_tx_num(tx_, block_num_, provider_); - block_changed_ = false; + + block_changed = true; } const TransactionNums txn_nums{ @@ -171,7 +169,7 @@ Task> PaginatedTransactionInfoIterator::next() { .block_num = block_num_, .txn_index = tnx_id - min_txn_num_ - 1, .final_txn = tnx_id == max_txn_num_, - .block_changed = block_changed_}; + .block_changed = block_changed}; SILK_DEBUG << "txn_id: " << txn_nums.txn_id << ", block_num: " << txn_nums.block_num diff --git a/silkworm/db/kv/txn_num.hpp b/silkworm/db/kv/txn_num.hpp index c59f8cccb9..bd872f4ad2 100644 --- a/silkworm/db/kv/txn_num.hpp +++ b/silkworm/db/kv/txn_num.hpp @@ -84,7 +84,6 @@ class PaginatedTransactionInfoIterator : public kv::api::PaginatedIterator make_txn_nums_stream(PaginatedTimestampStream stream, diff --git a/silkworm/rpc/core/logs_walker.cpp b/silkworm/rpc/core/logs_walker.cpp index bc161e1f7d..d9b1fc45a9 100644 --- a/silkworm/rpc/core/logs_walker.cpp +++ b/silkworm/rpc/core/logs_walker.cpp @@ -135,8 +135,7 @@ Task LogsWalker::get_logs(BlockNum start, paginated_stream = db::kv::api::make_range_stream(from_timestamp, to_timestamp); } - std::map receipts; - + Receipts receipts; uint64_t block_count{0}; uint64_t log_count{0}; Logs filtered_chunk_logs; @@ -154,12 +153,8 @@ Task LogsWalker::get_logs(BlockNum start, SILK_DEBUG << "Not found block no. " << tnx_nums->block_num; break; } - auto rr = co_await core::get_receipts(tx_, *block_with_hash, *chain_storage, workers_); - SILK_DEBUG << "Read #" << rr.size() << " receipts from block " << tnx_nums->block_num; - - std::for_each(rr.begin(), rr.end(), [&receipts](const auto& item) { - receipts[item.tx_hash] = std::move(item); - }); + receipts = co_await core::get_receipts(tx_, *block_with_hash, *chain_storage, workers_); + SILK_DEBUG << "Read #" << receipts.size() << " receipts from block " << tnx_nums->block_num; ++block_count; } @@ -171,7 +166,8 @@ Task LogsWalker::get_logs(BlockNum start, SILK_DEBUG << "Got transaction: block_num: " << tnx_nums->block_num << ", txn_index: " << tnx_nums->txn_index; - const auto& receipt = receipts.at(transaction.value().hash()); + SILKWORM_ASSERT(tnx_nums->txn_index < receipts.size()); + const auto& receipt = receipts.at(tnx_nums->txn_index); SILK_DEBUG << "#rawLogs: " << receipt.logs.size(); filtered_chunk_logs.clear();