Skip to content

Commit

Permalink
move block cache from rpcdaemon to core (#1323)
Browse files Browse the repository at this point in the history
small cleanups
  • Loading branch information
canepat committed Jul 12, 2023
1 parent a3cfbe5 commit fec13ca
Show file tree
Hide file tree
Showing 17 changed files with 25 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@
#pragma once

#include <cstddef>
#include <mutex>
#include <optional>
#include <memory>

#include <evmc/evmc.hpp>

#include <silkworm/core/chain/config.hpp>
#include <silkworm/core/common/base.hpp>
#include <silkworm/core/common/lru_cache.hpp>
#include <silkworm/core/common/util.hpp>
#include <silkworm/core/execution/address.hpp>
#include <silkworm/core/types/receipt.hpp>
#include <silkworm/core/types/transaction.hpp>
#include <silkworm/node/db/util.hpp>
#include <silkworm/core/types/block.hpp>

namespace silkworm {

Expand All @@ -38,16 +32,16 @@ class BlockCache {
explicit BlockCache(std::size_t capacity = 1024, bool shared_cache = true)
: block_cache_(capacity, shared_cache) {}

std::optional<std::shared_ptr<silkworm::BlockWithHash>> get(const evmc::bytes32& key) {
std::optional<std::shared_ptr<BlockWithHash>> get(const evmc::bytes32& key) {
return block_cache_.get_as_copy(key);
}

void insert(const evmc::bytes32& key, const std::shared_ptr<silkworm::BlockWithHash> block) {
void insert(const evmc::bytes32& key, const std::shared_ptr<BlockWithHash>& block) {
block_cache_.put(key, block);
}

private:
lru_cache<evmc::bytes32, std::shared_ptr<silkworm::BlockWithHash>> block_cache_;
lru_cache<evmc::bytes32, std::shared_ptr<BlockWithHash>> block_cache_;
};

} // namespace silkworm
File renamed without changes.
1 change: 1 addition & 0 deletions silkworm/core/types/block_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "block.hpp"

#include <catch2/catch.hpp>
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/commands/debug_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#include <boost/asio/thread_pool.hpp>
#include <nlohmann/json.hpp>

#include <silkworm/core/common/block_cache.hpp>
#include <silkworm/infra/concurrency/private_service.hpp>
#include <silkworm/infra/concurrency/shared_service.hpp>
#include <silkworm/silkrpc/common/block_cache.hpp>
#include <silkworm/silkrpc/core/rawdb/accessors.hpp>
#include <silkworm/silkrpc/ethdb/database.hpp>
#include <silkworm/silkrpc/ethdb/kv/state_cache.hpp>
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/commands/erigon_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#include <boost/asio/io_context.hpp>
#include <nlohmann/json.hpp>

#include <silkworm/core/common/block_cache.hpp>
#include <silkworm/infra/concurrency/private_service.hpp>
#include <silkworm/infra/concurrency/shared_service.hpp>
#include <silkworm/silkrpc/common/block_cache.hpp>
#include <silkworm/silkrpc/core/rawdb/accessors.hpp>
#include <silkworm/silkrpc/ethbackend/backend.hpp>
#include <silkworm/silkrpc/ethdb/database.hpp>
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/commands/eth_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#include <evmc/evmc.hpp>
#include <nlohmann/json.hpp>

#include <silkworm/core/common/block_cache.hpp>
#include <silkworm/core/types/receipt.hpp>
#include <silkworm/infra/concurrency/private_service.hpp>
#include <silkworm/infra/concurrency/shared_service.hpp>
#include <silkworm/silkrpc/common/block_cache.hpp>
#include <silkworm/silkrpc/core/filter_storage.hpp>
#include <silkworm/silkrpc/core/rawdb/accessors.hpp>
#include <silkworm/silkrpc/ethbackend/backend.hpp>
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/commands/ots_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#include <nlohmann/json.hpp>

#include <silkworm/core/common/base.hpp>
#include <silkworm/core/common/block_cache.hpp>
#include <silkworm/infra/concurrency/private_service.hpp>
#include <silkworm/infra/concurrency/shared_service.hpp>
#include <silkworm/silkrpc/common/block_cache.hpp>
#include <silkworm/silkrpc/ethbackend/backend.hpp>
#include <silkworm/silkrpc/ethdb/database.hpp>
#include <silkworm/silkrpc/ethdb/kv/state_cache.hpp>
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/commands/parity_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#include <boost/asio/io_context.hpp>
#include <nlohmann/json.hpp>

#include <silkworm/core/common/block_cache.hpp>
#include <silkworm/infra/concurrency/private_service.hpp>
#include <silkworm/infra/concurrency/shared_service.hpp>
#include <silkworm/silkrpc/common/block_cache.hpp>
#include <silkworm/silkrpc/core/rawdb/accessors.hpp>
#include <silkworm/silkrpc/ethdb/database.hpp>
#include <silkworm/silkrpc/json/types.hpp>
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/commands/trace_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#include <boost/asio/thread_pool.hpp>
#include <nlohmann/json.hpp>

#include <silkworm/core/common/block_cache.hpp>
#include <silkworm/infra/concurrency/private_service.hpp>
#include <silkworm/infra/concurrency/shared_service.hpp>
#include <silkworm/silkrpc/common/block_cache.hpp>
#include <silkworm/silkrpc/core/rawdb/accessors.hpp>
#include <silkworm/silkrpc/ethdb/database.hpp>
#include <silkworm/silkrpc/ethdb/kv/state_cache.hpp>
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/core/cached_chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <boost/asio/awaitable.hpp>
#include <evmc/evmc.hpp>

#include <silkworm/silkrpc/common/block_cache.hpp>
#include <silkworm/core/common/block_cache.hpp>
#include <silkworm/silkrpc/core/rawdb/accessors.hpp>
#include <silkworm/silkrpc/types/block.hpp>
#include <silkworm/silkrpc/types/transaction.hpp>
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/core/call_many.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
#include <boost/asio/thread_pool.hpp>
#include <nlohmann/json.hpp>

#include <silkworm/core/common/block_cache.hpp>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#include <silkworm/core/execution/evm.hpp>
#pragma GCC diagnostic pop
#include <silkworm/core/state/intra_block_state.hpp>
#include <silkworm/silkrpc/common/block_cache.hpp>
#include <silkworm/silkrpc/core/evm_executor.hpp>
#include <silkworm/silkrpc/core/rawdb/accessors.hpp>
#include <silkworm/silkrpc/ethdb/kv/state_cache.hpp>
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/core/evm_debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
#include <gsl/narrow>
#include <nlohmann/json.hpp>

#include <silkworm/core/common/block_cache.hpp>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#include <silkworm/core/execution/evm.hpp>
#pragma GCC diagnostic pop
#include <silkworm/core/state/intra_block_state.hpp>
#include <silkworm/silkrpc/common/block_cache.hpp>
#include <silkworm/silkrpc/core/rawdb/accessors.hpp>
#include <silkworm/silkrpc/ethdb/transaction.hpp>
#include <silkworm/silkrpc/ethdb/transaction_database.hpp>
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/core/evm_trace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
#include <gsl/narrow>
#include <nlohmann/json.hpp>

#include <silkworm/core/common/block_cache.hpp>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#include <silkworm/core/execution/evm.hpp>
#pragma GCC diagnostic pop
#include <silkworm/core/state/intra_block_state.hpp>
#include <silkworm/silkrpc/common/block_cache.hpp>
#include <silkworm/silkrpc/core/evm_executor.hpp>
#include <silkworm/silkrpc/core/rawdb/accessors.hpp>
#include <silkworm/silkrpc/ethdb/transaction.hpp>
Expand Down
1 change: 1 addition & 0 deletions silkworm/silkrpc/daemon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <silkworm/infra/common/log.hpp>
#include <silkworm/infra/grpc/client/client_context_pool.hpp>
#include <silkworm/infra/grpc/common/version.hpp>
#include <silkworm/node/db/mdbx.hpp>
#include <silkworm/silkrpc/common/constants.hpp>
#include <silkworm/silkrpc/ethdb/kv/state_changes_stream.hpp>
#include <silkworm/silkrpc/http/server.hpp>
Expand Down
7 changes: 5 additions & 2 deletions silkworm/silkrpc/http/request_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ boost::asio::awaitable<void> RequestHandler::handle(const http::Request& request
reply.content = "";
reply.status = http::StatusType::no_content;
} else {
SILK_DEBUG << "handle_user_request content: " << request.content;
SILK_TRACE << "handle HTTP request content #size: " << request.content.size();

const auto request_json = nlohmann::json::parse(request.content);

Expand Down Expand Up @@ -97,7 +97,7 @@ boost::asio::awaitable<void> RequestHandler::handle(const http::Request& request

co_await do_write(reply);

SILK_INFO << "handle_user_request t=" << clock_time::since(start) << "ns";
SILK_TRACE << "handle HTTP request t=" << clock_time::since(start) << "ns";
}

boost::asio::awaitable<void> RequestHandler::handle_request_and_create_reply(const nlohmann::json& request_json, http::Reply& reply) {
Expand All @@ -119,16 +119,19 @@ boost::asio::awaitable<void> RequestHandler::handle_request_and_create_reply(con
const auto json_glaze_handler = rpc_api_table_.find_json_glaze_handler(method);
if (json_glaze_handler) {
co_await handle_request(request_id, *json_glaze_handler, request_json, reply);
SILK_TRACE << "handle RPC request: " << method;
co_return;
}
const auto json_handler = rpc_api_table_.find_json_handler(method);
if (json_handler) {
co_await handle_request(request_id, *json_handler, request_json, reply);
SILK_TRACE << "handle RPC request: " << method;
co_return;
}
const auto stream_handler = rpc_api_table_.find_stream_handler(method);
if (stream_handler) {
co_await handle_request(*stream_handler, request_json);
SILK_TRACE << "handle RPC stream request: " << method;
co_return;
}

Expand Down
5 changes: 2 additions & 3 deletions silkworm/silkrpc/http/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

#include <silkworm/infra/common/log.hpp>
#include <silkworm/silkrpc/common/constants.hpp>
#include <silkworm/silkrpc/common/util.hpp>
#include <silkworm/silkrpc/http/connection.hpp>

namespace silkworm::rpc::http {
Expand Down Expand Up @@ -70,7 +69,7 @@ Server::Server(const std::string& end_point,
}

void Server::start() {
boost::asio::co_spawn(acceptor_.get_executor(), run(), [&](std::exception_ptr eptr) {
boost::asio::co_spawn(acceptor_.get_executor(), run(), [&](const std::exception_ptr& eptr) {
if (eptr) std::rethrow_exception(eptr);
});
}
Expand All @@ -94,7 +93,7 @@ boost::asio::awaitable<void> Server::run() {
SILK_TRACE << "Server::run starting connection for socket: " << &new_connection->socket();
auto connection_loop = [=]() -> boost::asio::awaitable<void> { co_await new_connection->read_loop(); };

boost::asio::co_spawn(io_context_, connection_loop, [&](std::exception_ptr eptr) {
boost::asio::co_spawn(io_context_, connection_loop, [&](const std::exception_ptr& eptr) {
if (eptr) std::rethrow_exception(eptr);
});
}
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/test/context_test_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

#include "context_test_base.hpp"

#include <silkworm/core/common/block_cache.hpp>
#include <silkworm/infra/concurrency/private_service.hpp>
#include <silkworm/infra/concurrency/shared_service.hpp>
#include <silkworm/silkrpc/common/block_cache.hpp>
#include <silkworm/silkrpc/core/filter_storage.hpp>
#include <silkworm/silkrpc/ethbackend/remote_backend.hpp>
#include <silkworm/silkrpc/ethdb/kv/remote_database.hpp>
Expand Down

0 comments on commit fec13ca

Please sign in to comment.