From f0cfe2117f8e63d8a459554874d89fe42d98e752 Mon Sep 17 00:00:00 2001 From: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com> Date: Fri, 5 Jan 2024 10:15:59 +0100 Subject: [PATCH] sentry: treat clang-tidy warnings as errors (#1732) --- silkworm/sentry/CMakeLists.txt | 4 ++++ silkworm/sentry/common/random.hpp | 2 +- .../sentry/discovery/common/node_address.cpp | 2 +- .../sentry/discovery/disc_v4/discovery.cpp | 6 ++--- .../disc_v4/enr/enr_response_message_test.cpp | 4 ++-- .../disc_v4/enr/fetch_enr_record.cpp | 2 +- .../discovery/disc_v4/find/find_neighbors.cpp | 2 +- .../disc_v4/find/find_node_message.cpp | 2 +- .../sentry/discovery/disc_v4/find/lookup.cpp | 2 +- .../disc_v4/find/neighbors_message.cpp | 4 ++-- .../discovery/disc_v4/ping/ping_check.cpp | 10 ++++---- .../discovery/disc_v4/ping/ping_message.cpp | 8 +++---- .../discovery/disc_v4/ping/pong_message.cpp | 6 ++--- silkworm/sentry/discovery/disc_v4/server.cpp | 2 +- silkworm/sentry/discovery/discovery.cpp | 14 ++++++----- silkworm/sentry/discovery/enr/enr_codec.cpp | 14 +++++------ .../discovery/node_db/node_db_sqlite.cpp | 2 +- .../discovery/node_db/serial_node_db.cpp | 24 +++++++++---------- silkworm/sentry/eth/status_data.hpp | 2 +- silkworm/sentry/eth/status_message.hpp | 4 ++-- .../sentry/grpc/interfaces/eth_version.cpp | 4 ++-- silkworm/sentry/grpc/interfaces/message.hpp | 2 +- silkworm/sentry/grpc/interfaces/peer_info.hpp | 6 ++--- silkworm/sentry/message_sender.cpp | 2 +- silkworm/sentry/multi_sentry_client.cpp | 4 ++-- silkworm/sentry/peer_manager_api.cpp | 6 ++--- silkworm/sentry/rlpx/auth/handshake.hpp | 9 ++++--- .../sentry/rlpx/common/disconnect_message.cpp | 2 +- silkworm/sentry/rlpx/crypto/aes.cpp | 2 +- silkworm/sentry/rlpx/crypto/sha3_hasher.cpp | 3 +-- .../sentry/rlpx/framing/framing_cipher.cpp | 4 +--- .../rlpx/framing/message_frame_codec.cpp | 6 ++--- silkworm/sentry/rlpx/peer.cpp | 4 ++-- silkworm/sentry/sentry_client_factory.cpp | 4 ++-- silkworm/sentry/sentry_client_factory.hpp | 2 +- silkworm/sentry/session_sentry_client.cpp | 4 ++-- 36 files changed, 93 insertions(+), 87 deletions(-) diff --git a/silkworm/sentry/CMakeLists.txt b/silkworm/sentry/CMakeLists.txt index 3326d0951a..57f6638f1c 100644 --- a/silkworm/sentry/CMakeLists.txt +++ b/silkworm/sentry/CMakeLists.txt @@ -23,6 +23,10 @@ find_package(Microsoft.GSL REQUIRED) find_package(OpenSSL REQUIRED) find_package(Snappy REQUIRED) +if(SILKWORM_CLANG_TIDY) + set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY};-warnings-as-errors=*") +endif() + # sentry common add_subdirectory(common) diff --git a/silkworm/sentry/common/random.hpp b/silkworm/sentry/common/random.hpp index 9c99cf9bb1..f908d7c026 100644 --- a/silkworm/sentry/common/random.hpp +++ b/silkworm/sentry/common/random.hpp @@ -55,7 +55,7 @@ std::list random_list_items(std::list& l, size_t max_count) { BackInsertPtrIterator& operator*() { return *this; } BackInsertPtrIterator& operator++() { return *this; } - BackInsertPtrIterator operator++(int) { return *this; } + BackInsertPtrIterator operator++(int) { return *this; } // NOLINT(cert-dcl21-cpp) private: std::list* container_; diff --git a/silkworm/sentry/discovery/common/node_address.cpp b/silkworm/sentry/discovery/common/node_address.cpp index 6a0220bec3..d2557425a0 100644 --- a/silkworm/sentry/discovery/common/node_address.cpp +++ b/silkworm/sentry/discovery/common/node_address.cpp @@ -63,7 +63,7 @@ void encode(Bytes& to, const NodeAddress& address) { //! RLP decode DecodingResult decode(ByteView& from, NodeAddress& to, rlp::Leftover mode) noexcept { Bytes ip_bytes; - uint16_t port; + uint16_t port{0}; auto result = rlp::decode(from, mode, ip_bytes, port, to.port_rlpx); if (!result) { return result; diff --git a/silkworm/sentry/discovery/disc_v4/discovery.cpp b/silkworm/sentry/discovery/disc_v4/discovery.cpp index f878edf2db..3065879c1c 100644 --- a/silkworm/sentry/discovery/disc_v4/discovery.cpp +++ b/silkworm/sentry/discovery/disc_v4/discovery.cpp @@ -46,7 +46,7 @@ class DiscoveryImpl : private MessageHandler { DiscoveryImpl( const boost::asio::any_io_executor& executor, uint16_t server_port, - std::function node_key, + std::function node_key, // NOLINT(performance-unnecessary-value-param) std::function node_url, std::function node_record, node_db::NodeDb& node_db) @@ -110,7 +110,7 @@ class DiscoveryImpl : private MessageHandler { Task on_enr_request(enr::EnrRequestMessage message, EccPublicKey sender_public_key, boost::asio::ip::udp::endpoint sender_endpoint, Bytes packet_hash) override { return enr::EnrRequestHandler::handle( - std::move(message), + message, std::move(sender_public_key), std::move(sender_endpoint), std::move(packet_hash), @@ -157,7 +157,7 @@ class DiscoveryImpl : private MessageHandler { for (auto& node_id : node_ids) { // grab the semaphore and block once we reach kPingChecksTasksMax co_await ping_checks_semaphore_.send(node_id.serialized()); - ping_checks_tasks_.spawn(executor, [this, node_id = std::move(node_id)]() -> Task { + ping_checks_tasks_.spawn(executor, [this, node_id = std::move(node_id)]() mutable -> Task { // when a ping check is going to finish, unblock the semaphore [[maybe_unused]] auto _ = gsl::finally([this] { auto finished_task_id = this->ping_checks_semaphore_.try_receive(); diff --git a/silkworm/sentry/discovery/disc_v4/enr/enr_response_message_test.cpp b/silkworm/sentry/discovery/disc_v4/enr/enr_response_message_test.cpp index 1a5f650089..d55f54f151 100644 --- a/silkworm/sentry/discovery/disc_v4/enr/enr_response_message_test.cpp +++ b/silkworm/sentry/discovery/disc_v4/enr/enr_response_message_test.cpp @@ -47,8 +47,8 @@ TEST_CASE("EnrResponseMessage.rlp_decode") { CHECK(eth1_fork_id_data_rlp_header->payload_length == 10); // now eth1_fork_id_data is just RLP([hash_u32, next_u64]) - uint32_t eth1_fork_id_hash; - uint64_t eth1_fork_id_next; + uint32_t eth1_fork_id_hash{0}; + uint64_t eth1_fork_id_next{0}; auto eth1_fork_id_decode_result = rlp::decode(eth1_fork_id_data, rlp::Leftover::kProhibit, eth1_fork_id_hash, eth1_fork_id_next); CHECK(eth1_fork_id_decode_result.has_value()); if (!eth1_fork_id_decode_result) { diff --git a/silkworm/sentry/discovery/disc_v4/enr/fetch_enr_record.cpp b/silkworm/sentry/discovery/disc_v4/enr/fetch_enr_record.cpp index e19554c0ef..a82238aec6 100644 --- a/silkworm/sentry/discovery/disc_v4/enr/fetch_enr_record.cpp +++ b/silkworm/sentry/discovery/disc_v4/enr/fetch_enr_record.cpp @@ -56,7 +56,7 @@ Task> fetch_enr_record( }; try { - co_await message_sender.send_enr_request(std::move(request_message), endpoint); + co_await message_sender.send_enr_request(request_message, endpoint); } catch (const boost::system::system_error& ex) { if (ex.code() == boost::system::errc::operation_canceled) throw; diff --git a/silkworm/sentry/discovery/disc_v4/find/find_neighbors.cpp b/silkworm/sentry/discovery/disc_v4/find/find_neighbors.cpp index ad7f58d93d..394ccd8e86 100644 --- a/silkworm/sentry/discovery/disc_v4/find/find_neighbors.cpp +++ b/silkworm/sentry/discovery/disc_v4/find/find_neighbors.cpp @@ -53,7 +53,7 @@ Task find_neighbors( auto executor = co_await boost::asio::this_coro::executor; concurrency::Channel> neighbors_channel{executor, 2}; - auto on_neighbors_handler = [&](NeighborsMessage message, EccPublicKey sender_node_id) { + auto on_neighbors_handler = [&](NeighborsMessage message, const EccPublicKey& sender_node_id) { if ((sender_node_id == node_id) && !is_expired_message_expiration(message.expiration)) { neighbors_channel.try_send(std::move(message.node_addresses)); } diff --git a/silkworm/sentry/discovery/disc_v4/find/find_node_message.cpp b/silkworm/sentry/discovery/disc_v4/find/find_node_message.cpp index 10d8884822..6f9ac47a26 100644 --- a/silkworm/sentry/discovery/disc_v4/find/find_node_message.cpp +++ b/silkworm/sentry/discovery/disc_v4/find/find_node_message.cpp @@ -35,7 +35,7 @@ Bytes FindNodeMessage::rlp_encode() const { FindNodeMessage FindNodeMessage::rlp_decode(ByteView data) { Bytes target_public_key_data; - uint64_t expiration_ts; + uint64_t expiration_ts{0}; auto result = rlp::decode( data, diff --git a/silkworm/sentry/discovery/disc_v4/find/lookup.cpp b/silkworm/sentry/discovery/disc_v4/find/lookup.cpp index 3fff6be591..32b78bda45 100644 --- a/silkworm/sentry/discovery/disc_v4/find/lookup.cpp +++ b/silkworm/sentry/discovery/disc_v4/find/lookup.cpp @@ -45,7 +45,7 @@ Task lookup( /* max_lookup_time = */ now - 10min, /* limit = */ 3, }; - auto node_ids = co_await db.take_lookup_candidates(std::move(query), now); + auto node_ids = co_await db.take_lookup_candidates(query, now); size_t total_neighbors = 0; auto group_task = concurrency::generate_parallel_group_task(node_ids.size(), [&](size_t index) -> Task { diff --git a/silkworm/sentry/discovery/disc_v4/find/neighbors_message.cpp b/silkworm/sentry/discovery/disc_v4/find/neighbors_message.cpp index e326a69992..eeac634b24 100644 --- a/silkworm/sentry/discovery/disc_v4/find/neighbors_message.cpp +++ b/silkworm/sentry/discovery/disc_v4/find/neighbors_message.cpp @@ -49,7 +49,7 @@ void encode(Bytes& to, const NeighborsNodeInfo& info) { //! RLP decode NeighborsNodeInfo DecodingResult decode(ByteView& from, NeighborsNodeInfo& to, rlp::Leftover mode) noexcept { Bytes ip_bytes; - uint16_t port; + uint16_t port{0}; Bytes public_key_data; auto result = rlp::decode(from, mode, ip_bytes, port, to.address.port_rlpx, public_key_data); if (!result) { @@ -88,7 +88,7 @@ Bytes NeighborsMessage::rlp_encode() const { NeighborsMessage NeighborsMessage::rlp_decode(ByteView data) { std::vector node_infos; - uint64_t expiration_ts; + uint64_t expiration_ts{0}; auto result = rlp::decode( data, diff --git a/silkworm/sentry/discovery/disc_v4/ping/ping_check.cpp b/silkworm/sentry/discovery/disc_v4/ping/ping_check.cpp index ae478b7b22..79e4bf5f67 100644 --- a/silkworm/sentry/discovery/disc_v4/ping/ping_check.cpp +++ b/silkworm/sentry/discovery/disc_v4/ping/ping_check.cpp @@ -79,7 +79,7 @@ Task ping_check( local_enr_seq_num, message_sender, on_pong_signal, - std::move(last_pong_time), + last_pong_time, ping_fails_count.value_or(0)); co_return result; @@ -119,7 +119,7 @@ Task ping_check( auto executor = co_await boost::asio::this_coro::executor; concurrency::EventNotifier pong_received_notifier{executor}; std::optional enr_seq_num; - auto on_pong_handler = [&](PongMessage message, EccPublicKey sender_node_id) { + auto on_pong_handler = [&](const PongMessage& message, const EccPublicKey& sender_node_id) { if ((sender_node_id == node_id) && !is_expired_message_expiration(message.expiration)) { enr_seq_num = message.enr_seq_num; pong_received_notifier.notify(); @@ -164,10 +164,10 @@ Task ping_check( co_return PingCheckResult{ std::move(node_id), - std::move(pong_time), + pong_time, ping_fails_count, - std::move(next_ping_time1), - std::move(enr_seq_num), + next_ping_time1, + enr_seq_num, }; } diff --git a/silkworm/sentry/discovery/disc_v4/ping/ping_message.cpp b/silkworm/sentry/discovery/disc_v4/ping/ping_message.cpp index ef22e02e61..c0b4f637cd 100644 --- a/silkworm/sentry/discovery/disc_v4/ping/ping_message.cpp +++ b/silkworm/sentry/discovery/disc_v4/ping/ping_message.cpp @@ -39,10 +39,10 @@ Bytes PingMessage::rlp_encode() const { } PingMessage PingMessage::rlp_decode(ByteView data) { - unsigned int disc_version; + unsigned int disc_version{0}; NodeAddress sender_address; NodeAddress recipient_address; - uint64_t expiration_ts; + uint64_t expiration_ts{0}; std::optional enr_seq_num_opt; auto result = rlp::decode( @@ -56,7 +56,7 @@ PingMessage PingMessage::rlp_decode(ByteView data) { throw DecodingException(result.error(), "Failed to decode PingMessage RLP"); } - uint64_t enr_seq_num; + uint64_t enr_seq_num{0}; if (rlp::decode(data, enr_seq_num)) { enr_seq_num_opt = enr_seq_num; } @@ -66,7 +66,7 @@ PingMessage PingMessage::rlp_decode(ByteView data) { sender_address.port_rlpx, std::move(recipient_address.endpoint), time_point_from_unix_timestamp(expiration_ts), - std::move(enr_seq_num_opt), + enr_seq_num_opt, }; } diff --git a/silkworm/sentry/discovery/disc_v4/ping/pong_message.cpp b/silkworm/sentry/discovery/disc_v4/ping/pong_message.cpp index e6e1ecdb68..f2e5e76a46 100644 --- a/silkworm/sentry/discovery/disc_v4/ping/pong_message.cpp +++ b/silkworm/sentry/discovery/disc_v4/ping/pong_message.cpp @@ -39,7 +39,7 @@ Bytes PongMessage::rlp_encode() const { PongMessage PongMessage::rlp_decode(ByteView data) { NodeAddress recipient_address; Bytes ping_hash; - uint64_t expiration_ts; + uint64_t expiration_ts{0}; std::optional enr_seq_num_opt; auto result = rlp::decode( @@ -52,7 +52,7 @@ PongMessage PongMessage::rlp_decode(ByteView data) { throw DecodingException(result.error(), "Failed to decode PingMessage RLP"); } - uint64_t enr_seq_num; + uint64_t enr_seq_num{0}; if (rlp::decode(data, enr_seq_num)) { enr_seq_num_opt = enr_seq_num; } @@ -61,7 +61,7 @@ PongMessage PongMessage::rlp_decode(ByteView data) { std::move(recipient_address.endpoint), std::move(ping_hash), time_point_from_unix_timestamp(expiration_ts), - std::move(enr_seq_num_opt), + enr_seq_num_opt, }; } diff --git a/silkworm/sentry/discovery/disc_v4/server.cpp b/silkworm/sentry/discovery/disc_v4/server.cpp index c778d7f2dd..193bcea51f 100644 --- a/silkworm/sentry/discovery/disc_v4/server.cpp +++ b/silkworm/sentry/discovery/disc_v4/server.cpp @@ -221,7 +221,7 @@ Task Server::send_neighbors(find::NeighborsMessage message, ip::udp::endpo } Task Server::send_enr_request(enr::EnrRequestMessage message, ip::udp::endpoint recipient) { - return p_impl_->send_message(std::move(message), std::move(recipient)); + return p_impl_->send_message(message, std::move(recipient)); } Task Server::send_enr_response(enr::EnrResponseMessage message, ip::udp::endpoint recipient) { diff --git a/silkworm/sentry/discovery/discovery.cpp b/silkworm/sentry/discovery/discovery.cpp index acd3aec92c..fbae1e2d42 100644 --- a/silkworm/sentry/discovery/discovery.cpp +++ b/silkworm/sentry/discovery/discovery.cpp @@ -38,7 +38,7 @@ class DiscoveryImpl { concurrency::ExecutorPool& executor_pool, std::vector peer_urls, bool with_dynamic_discovery, - const std::filesystem::path& data_dir_path, + std::filesystem::path data_dir_path, uint64_t network_id, std::function node_key, std::function node_url, @@ -77,21 +77,21 @@ DiscoveryImpl::DiscoveryImpl( concurrency::ExecutorPool& executor_pool, std::vector peer_urls, bool with_dynamic_discovery, - const std::filesystem::path& data_dir_path, + std::filesystem::path data_dir_path, uint64_t network_id, - std::function node_key, + std::function node_key, // NOLINT(performance-unnecessary-value-param) std::function node_url, std::function node_record, std::vector bootnodes, uint16_t disc_v4_port) : peer_urls_(std::move(peer_urls)), with_dynamic_discovery_(with_dynamic_discovery), - data_dir_path_(data_dir_path), + data_dir_path_(std::move(data_dir_path)), node_id_([node_key] { return node_key().public_key(); }), network_id_(network_id), node_db_(executor_pool.any_executor()), bootnodes_(std::move(bootnodes)), - disc_v4_discovery_(executor_pool.any_executor(), disc_v4_port, node_key, node_url, node_record, node_db_.interface()) { + disc_v4_discovery_(executor_pool.any_executor(), disc_v4_port, node_key, std::move(node_url), std::move(node_record), node_db_.interface()) { } Task DiscoveryImpl::run() { @@ -139,8 +139,10 @@ Task> DiscoveryImpl::request_peer_candidat using namespace std::chrono_literals; std::vector exclude_ids; - for (auto& url : exclude_urls) + exclude_ids.reserve(exclude_urls.size()); + for (auto& url : exclude_urls) { exclude_ids.push_back(url.public_key()); + } auto now = std::chrono::system_clock::now(); node_db::NodeDb::FindPeerCandidatesQuery query{ diff --git a/silkworm/sentry/discovery/enr/enr_codec.cpp b/silkworm/sentry/discovery/enr/enr_codec.cpp index 2022c8d56f..0f9c1627b5 100644 --- a/silkworm/sentry/discovery/enr/enr_codec.cpp +++ b/silkworm/sentry/discovery/enr/enr_codec.cpp @@ -83,17 +83,17 @@ static std::optional try_decode_node_address( const char* ip_key, const char* port_disc_key, const char* port_rlpx_key) { - if ((entries_data.count(ip_key) == 0) || (entries_data.count(port_disc_key) == 0)) + if (!entries_data.contains(ip_key) || !entries_data.contains(port_disc_key)) return std::nullopt; auto ip = ip_address_from_bytes(decode_rlp_bytes(entries_data.at(ip_key), ip_key)); if (!ip) throw std::runtime_error("EnrCodec: invalid IP address"); - uint16_t port_disc = decode_rlp_num_value(entries_data.at(port_disc_key), port_disc_key); + auto port_disc = decode_rlp_num_value(entries_data.at(port_disc_key), port_disc_key); uint16_t port_rlpx = 0; - if (entries_data.count(port_rlpx_key) > 0) { + if (entries_data.contains(port_rlpx_key)) { port_rlpx = decode_rlp_num_value(entries_data.at(port_rlpx_key), port_rlpx_key); } @@ -140,7 +140,7 @@ EnrRecord EnrCodec::decode(ByteView data) { items.pop_back(); auto& seq_num_data = items[1]; - uint64_t seq_num = decode_rlp_num_value(seq_num_data, "seq_num"); + auto seq_num = decode_rlp_num_value(seq_num_data, "seq_num"); std::map entries_data; for (size_t i = 2; i < items.size(); i += 2) { @@ -149,12 +149,12 @@ EnrRecord EnrCodec::decode(ByteView data) { entries_data.emplace(key, items[i + 1]); } - if (entries_data.count("id") == 0) + if (!entries_data.contains("id")) throw std::runtime_error("EnrCodec: missing required 'id' key"); if (decode_rlp_bytes(entries_data.at("id"), "id") != Bytes{'v', '4'}) throw std::runtime_error("EnrCodec: unsupported ID scheme"); - if (entries_data.count("secp256k1") == 0) + if (!entries_data.contains("secp256k1")) throw std::runtime_error("EnrCodec: missing required 'secp256k1' key"); auto public_key = EccPublicKey::deserialize_std(decode_rlp_bytes(entries_data.at("secp256k1"), "secp256k1")); @@ -220,7 +220,7 @@ Bytes EnrCodec::encode(const EnrRecord& record, const EccKeyPair& key_pair) { // set signature Bytes signature = sign(items, key_pair.private_key()); - items[0] = encode_rlp_bytes(std::move(signature)); + items[0] = encode_rlp_bytes(signature); Bytes data; rlp::encode(data, items); diff --git a/silkworm/sentry/discovery/node_db/node_db_sqlite.cpp b/silkworm/sentry/discovery/node_db/node_db_sqlite.cpp index 18dc31f6b1..fd6a8125c0 100644 --- a/silkworm/sentry/discovery/node_db/node_db_sqlite.cpp +++ b/silkworm/sentry/discovery/node_db/node_db_sqlite.cpp @@ -493,7 +493,7 @@ class NodeDbSqliteImpl : public NodeDb { Task> take_lookup_candidates(FindLookupCandidatesQuery query, Time time) override { SQLite::Transaction transaction{*db_}; - auto candidates = co_await find_lookup_candidates(std::move(query)); + auto candidates = co_await find_lookup_candidates(query); co_await mark_taken_lookup_candidates(candidates, time); transaction.commit(); co_return candidates; diff --git a/silkworm/sentry/discovery/node_db/serial_node_db.cpp b/silkworm/sentry/discovery/node_db/serial_node_db.cpp index 8beb7d4a35..61805dff6c 100644 --- a/silkworm/sentry/discovery/node_db/serial_node_db.cpp +++ b/silkworm/sentry/discovery/node_db/serial_node_db.cpp @@ -37,7 +37,7 @@ Task> SerialNodeDb::find_node_address_v6(NodeId id) { } Task SerialNodeDb::update_next_ping_time(NodeId id, Time value) { - return concurrency::co_spawn_sw(strand_, db_.update_next_ping_time(std::move(id), std::move(value)), use_awaitable); + return concurrency::co_spawn_sw(strand_, db_.update_next_ping_time(std::move(id), value), use_awaitable); } Task> SerialNodeDb::find_next_ping_time(NodeId id) { @@ -45,7 +45,7 @@ Task> SerialNodeDb::find_next_ping_time(NodeId id) { } Task SerialNodeDb::update_last_pong_time(NodeId id, Time value) { - return concurrency::co_spawn_sw(strand_, db_.update_last_pong_time(std::move(id), std::move(value)), use_awaitable); + return concurrency::co_spawn_sw(strand_, db_.update_last_pong_time(std::move(id), value), use_awaitable); } Task> SerialNodeDb::find_last_pong_time(NodeId id) { @@ -53,7 +53,7 @@ Task> SerialNodeDb::find_last_pong_time(NodeId id) { } Task SerialNodeDb::update_ping_fails(NodeId id, size_t value) { - return concurrency::co_spawn_sw(strand_, db_.update_ping_fails(std::move(id), std::move(value)), use_awaitable); + return concurrency::co_spawn_sw(strand_, db_.update_ping_fails(std::move(id), value), use_awaitable); } Task> SerialNodeDb::find_ping_fails(NodeId id) { @@ -61,7 +61,7 @@ Task> SerialNodeDb::find_ping_fails(NodeId id) { } Task SerialNodeDb::update_peer_disconnected_time(NodeId id, Time value) { - return concurrency::co_spawn_sw(strand_, db_.update_peer_disconnected_time(std::move(id), std::move(value)), use_awaitable); + return concurrency::co_spawn_sw(strand_, db_.update_peer_disconnected_time(std::move(id), value), use_awaitable); } Task> SerialNodeDb::find_peer_disconnected_time(NodeId id) { @@ -69,7 +69,7 @@ Task> SerialNodeDb::find_peer_disconnected_time(NodeId id) { } Task SerialNodeDb::update_peer_is_useless(NodeId id, bool value) { - return concurrency::co_spawn_sw(strand_, db_.update_peer_is_useless(std::move(id), std::move(value)), use_awaitable); + return concurrency::co_spawn_sw(strand_, db_.update_peer_is_useless(std::move(id), value), use_awaitable); } Task> SerialNodeDb::find_peer_is_useless(NodeId id) { @@ -101,23 +101,23 @@ Task> SerialNodeDb::find_eth1_fork_id(NodeId id) { } Task> SerialNodeDb::find_ping_candidates(Time time, size_t limit) { - return concurrency::co_spawn_sw(strand_, db_.find_ping_candidates(std::move(time), limit), use_awaitable); + return concurrency::co_spawn_sw(strand_, db_.find_ping_candidates(time, limit), use_awaitable); } Task> SerialNodeDb::find_useful_nodes(Time min_pong_time, size_t limit) { - return concurrency::co_spawn_sw(strand_, db_.find_useful_nodes(std::move(min_pong_time), limit), use_awaitable); + return concurrency::co_spawn_sw(strand_, db_.find_useful_nodes(min_pong_time, limit), use_awaitable); } Task> SerialNodeDb::find_lookup_candidates(FindLookupCandidatesQuery query) { - return concurrency::co_spawn_sw(strand_, db_.find_lookup_candidates(std::move(query)), use_awaitable); + return concurrency::co_spawn_sw(strand_, db_.find_lookup_candidates(query), use_awaitable); } Task SerialNodeDb::mark_taken_lookup_candidates(const std::vector& ids, Time time) { - return concurrency::co_spawn_sw(strand_, db_.mark_taken_lookup_candidates(ids, std::move(time)), use_awaitable); + return concurrency::co_spawn_sw(strand_, db_.mark_taken_lookup_candidates(ids, time), use_awaitable); } Task> SerialNodeDb::take_lookup_candidates(FindLookupCandidatesQuery query, Time time) { - return concurrency::co_spawn_sw(strand_, db_.take_lookup_candidates(std::move(query), std::move(time)), use_awaitable); + return concurrency::co_spawn_sw(strand_, db_.take_lookup_candidates(query, time), use_awaitable); } Task> SerialNodeDb::find_peer_candidates(FindPeerCandidatesQuery query) { @@ -125,11 +125,11 @@ Task> SerialNodeDb::find_peer_candidates(FindPeerCandidatesQ } Task SerialNodeDb::mark_taken_peer_candidates(const std::vector& ids, Time time) { - return concurrency::co_spawn_sw(strand_, db_.mark_taken_peer_candidates(ids, std::move(time)), use_awaitable); + return concurrency::co_spawn_sw(strand_, db_.mark_taken_peer_candidates(ids, time), use_awaitable); } Task> SerialNodeDb::take_peer_candidates(FindPeerCandidatesQuery query, Time time) { - return concurrency::co_spawn_sw(strand_, db_.take_peer_candidates(std::move(query), std::move(time)), use_awaitable); + return concurrency::co_spawn_sw(strand_, db_.take_peer_candidates(std::move(query), time), use_awaitable); } Task SerialNodeDb::delete_node(NodeId id) { diff --git a/silkworm/sentry/eth/status_data.hpp b/silkworm/sentry/eth/status_data.hpp index 604a9fe69e..6cc5d452a8 100644 --- a/silkworm/sentry/eth/status_data.hpp +++ b/silkworm/sentry/eth/status_data.hpp @@ -27,7 +27,7 @@ namespace silkworm::sentry::eth { struct StatusData { std::vector fork_block_numbers; std::vector fork_block_times; - BlockNum head_block_num; + BlockNum head_block_num{0}; StatusMessage message; }; diff --git a/silkworm/sentry/eth/status_message.hpp b/silkworm/sentry/eth/status_message.hpp index d20d7eaa53..3c31bfe842 100644 --- a/silkworm/sentry/eth/status_message.hpp +++ b/silkworm/sentry/eth/status_message.hpp @@ -33,8 +33,8 @@ struct StatusMessage { [[nodiscard]] Message to_message() const; [[nodiscard]] static StatusMessage from_message(const Message& message); - uint8_t version; - uint64_t network_id; + uint8_t version{0}; + uint64_t network_id{0}; intx::uint256 total_difficulty; Bytes best_block_hash; Bytes genesis_hash; diff --git a/silkworm/sentry/grpc/interfaces/eth_version.cpp b/silkworm/sentry/grpc/interfaces/eth_version.cpp index 429d660688..8cfd2fb519 100644 --- a/silkworm/sentry/grpc/interfaces/eth_version.cpp +++ b/silkworm/sentry/grpc/interfaces/eth_version.cpp @@ -21,12 +21,12 @@ namespace silkworm::sentry::grpc::interfaces { namespace proto = ::sentry; uint8_t eth_version_from_protocol(proto::Protocol protocol) { - assert(proto::Protocol_MIN == proto::Protocol::ETH65); + static_assert(proto::Protocol_MIN == proto::Protocol::ETH65); return static_cast(protocol) + 65; } proto::Protocol protocol_from_eth_version(uint8_t version) { - assert(proto::Protocol_MIN == proto::Protocol::ETH65); + static_assert(proto::Protocol_MIN == proto::Protocol::ETH65); return static_cast(version - 65); } diff --git a/silkworm/sentry/grpc/interfaces/message.hpp b/silkworm/sentry/grpc/interfaces/message.hpp index 33d7432986..113c3a9310 100644 --- a/silkworm/sentry/grpc/interfaces/message.hpp +++ b/silkworm/sentry/grpc/interfaces/message.hpp @@ -28,7 +28,7 @@ ::sentry::OutboundMessageData outbound_data_from_message(const sentry::Message& sentry::Message message_from_inbound_message(const ::sentry::InboundMessage& message); ::sentry::InboundMessage inbound_message_from_message(const sentry::Message& message); -uint8_t message_id_from_proto_message_id(::sentry::MessageId message_id); +uint8_t message_id_from_proto_message_id(::sentry::MessageId proto_id); ::sentry::MessageId proto_message_id_from_message_id(uint8_t message_id); api::MessageIdSet message_id_set_from_messages_request(const ::sentry::MessagesRequest& request); diff --git a/silkworm/sentry/grpc/interfaces/peer_info.hpp b/silkworm/sentry/grpc/interfaces/peer_info.hpp index cbeb0ece64..ed7f9a9597 100644 --- a/silkworm/sentry/grpc/interfaces/peer_info.hpp +++ b/silkworm/sentry/grpc/interfaces/peer_info.hpp @@ -25,12 +25,12 @@ namespace silkworm::sentry::grpc::interfaces { api::PeerInfo peer_info_from_proto_peer_info(const types::PeerInfo& info); -types::PeerInfo proto_peer_info_from_peer_info(const api::PeerInfo& info); +types::PeerInfo proto_peer_info_from_peer_info(const api::PeerInfo&); api::PeerInfos peer_infos_from_proto_peers_reply(const ::sentry::PeersReply& reply); -::sentry::PeersReply proto_peers_reply_from_peer_infos(const api::PeerInfos& infos); +::sentry::PeersReply proto_peers_reply_from_peer_infos(const api::PeerInfos&); std::optional peer_info_opt_from_proto_peer_reply(const ::sentry::PeerByIdReply& reply); -::sentry::PeerByIdReply proto_peer_reply_from_peer_info_opt(const std::optional& info_opt); +::sentry::PeerByIdReply proto_peer_reply_from_peer_info_opt(const std::optional&); } // namespace silkworm::sentry::grpc::interfaces diff --git a/silkworm/sentry/message_sender.cpp b/silkworm/sentry/message_sender.cpp index 582bf41421..ce1f2b013a 100644 --- a/silkworm/sentry/message_sender.cpp +++ b/silkworm/sentry/message_sender.cpp @@ -29,7 +29,7 @@ Task MessageSender::run(PeerManager& peer_manager) { api::router::SendMessageCall::PeerKeys sent_peer_keys; - auto sender = [&message = call.message(), &sent_peer_keys, peer_filter = call.peer_filter()](std::shared_ptr peer) { + auto sender = [&message = call.message(), &sent_peer_keys, peer_filter = call.peer_filter()](const std::shared_ptr& peer) { auto key_opt = peer->peer_public_key(); if (key_opt && (!peer_filter.peer_public_key || (key_opt.value() == peer_filter.peer_public_key.value()))) { sent_peer_keys.push_back(key_opt.value()); diff --git a/silkworm/sentry/multi_sentry_client.cpp b/silkworm/sentry/multi_sentry_client.cpp index cc6bf9fdb4..70e9558e31 100644 --- a/silkworm/sentry/multi_sentry_client.cpp +++ b/silkworm/sentry/multi_sentry_client.cpp @@ -50,13 +50,13 @@ class MultiSentryClientImpl : public api::Service { std::function(std::shared_ptr)> callback) { using namespace concurrency::awaitable_wait_for_one; - std::function(size_t)> call_factory = [&clients, &callback](size_t index) -> Task { + auto call_factory = [&clients, &callback](size_t index) -> Task { auto client = clients[index]; auto service = co_await client->service(); co_await callback(service); }; - auto group_task = concurrency::generate_parallel_group_task(clients.size(), std::move(call_factory)); + auto group_task = concurrency::generate_parallel_group_task(clients.size(), call_factory); try { co_await (std::move(group_task) || concurrency::timeout(timeout)); diff --git a/silkworm/sentry/peer_manager_api.cpp b/silkworm/sentry/peer_manager_api.cpp index 3c2ec7a778..2cf8f672bc 100644 --- a/silkworm/sentry/peer_manager_api.cpp +++ b/silkworm/sentry/peer_manager_api.cpp @@ -91,7 +91,7 @@ Task PeerManagerApi::handle_peers_calls() { auto call = co_await peers_calls_channel_.receive(); api::PeerInfos peers; - co_await peer_manager_.enumerate_peers([&peers](std::shared_ptr peer) { + co_await peer_manager_.enumerate_peers([&peers](const std::shared_ptr& peer) { auto info_opt = make_peer_info(*peer); if (info_opt) { peers.push_back(info_opt.value()); @@ -109,7 +109,7 @@ Task PeerManagerApi::handle_peer_calls() { auto peer_public_key_opt = call.peer_public_key; std::optional info_opt; - co_await peer_manager_.enumerate_peers([&info_opt, &peer_public_key_opt](std::shared_ptr peer) { + co_await peer_manager_.enumerate_peers([&info_opt, &peer_public_key_opt](const std::shared_ptr& peer) { auto key_opt = peer->peer_public_key(); if (key_opt && peer_public_key_opt && (key_opt.value() == peer_public_key_opt.value())) { info_opt = make_peer_info(*peer); @@ -125,7 +125,7 @@ Task PeerManagerApi::handle_peer_penalize_calls() { while (true) { auto peer_public_key_opt = co_await peer_penalize_calls_channel_.receive(); - co_await peer_manager_.enumerate_peers([&peer_public_key_opt](std::shared_ptr peer) { + co_await peer_manager_.enumerate_peers([&peer_public_key_opt](const std::shared_ptr& peer) { auto key_opt = peer->peer_public_key(); if (key_opt && peer_public_key_opt && (key_opt.value() == peer_public_key_opt.value())) { peer->disconnect(rlpx::DisconnectReason::DisconnectRequested); diff --git a/silkworm/sentry/rlpx/auth/handshake.hpp b/silkworm/sentry/rlpx/auth/handshake.hpp index a8a38d4c9f..fc74ea29cf 100644 --- a/silkworm/sentry/rlpx/auth/handshake.hpp +++ b/silkworm/sentry/rlpx/auth/handshake.hpp @@ -23,6 +23,8 @@ #include +#include + #include #include #include @@ -71,9 +73,10 @@ class Handshake { class CapabilityMismatchError : public std::runtime_error { public: CapabilityMismatchError( - std::string required_capability_desc, - std::string peer_capabilities_desc) - : std::runtime_error("rlpx::auth::Handshake: no matching required capability " + required_capability_desc + " in " + peer_capabilities_desc) {} + const std::string& required_capability_desc, + const std::string& peer_capabilities_desc) + : std::runtime_error(absl::StrCat("rlpx::auth::Handshake: no matching required capability ", + required_capability_desc, " in ", peer_capabilities_desc)) {} }; private: diff --git a/silkworm/sentry/rlpx/common/disconnect_message.cpp b/silkworm/sentry/rlpx/common/disconnect_message.cpp index ae08d8a03c..b31a3e70ef 100644 --- a/silkworm/sentry/rlpx/common/disconnect_message.cpp +++ b/silkworm/sentry/rlpx/common/disconnect_message.cpp @@ -45,7 +45,7 @@ DisconnectMessage DisconnectMessage::rlp_decode(ByteView data) { // if RLP is not a list if (!result && (result.error() == DecodingError::kUnexpectedString)) { - uint8_t reason_num; + uint8_t reason_num{0}; result = rlp::decode(data, reason_num); if (result) { reason.push_back(reason_num); diff --git a/silkworm/sentry/rlpx/crypto/aes.cpp b/silkworm/sentry/rlpx/crypto/aes.cpp index 609a401548..1f539b424f 100644 --- a/silkworm/sentry/rlpx/crypto/aes.cpp +++ b/silkworm/sentry/rlpx/crypto/aes.cpp @@ -33,7 +33,7 @@ extern const size_t kAESBlockSize = AES_BLOCK_SIZE; AESCipher::AESCipher(ByteView key, std::optional iv, Direction direction) { assert(!iv || (iv->size() == kAESBlockSize)); - const EVP_CIPHER* mode; + const EVP_CIPHER* mode{nullptr}; switch (key.size()) { case kKeySize128: mode = iv ? EVP_aes_128_ctr() : EVP_aes_128_ecb(); diff --git a/silkworm/sentry/rlpx/crypto/sha3_hasher.cpp b/silkworm/sentry/rlpx/crypto/sha3_hasher.cpp index 442f6b4797..20238beef3 100644 --- a/silkworm/sentry/rlpx/crypto/sha3_hasher.cpp +++ b/silkworm/sentry/rlpx/crypto/sha3_hasher.cpp @@ -25,8 +25,7 @@ namespace silkworm::sentry::rlpx::crypto { Sha3Hasher::Sha3Hasher() : impl_(std::make_unique()) { } -Sha3Hasher::~Sha3Hasher() { -} +Sha3Hasher::~Sha3Hasher() = default; void Sha3Hasher::update(ByteView data) { impl_->add(data.data(), data.size()); diff --git a/silkworm/sentry/rlpx/framing/framing_cipher.cpp b/silkworm/sentry/rlpx/framing/framing_cipher.cpp index bcfca8d040..116b1ccae5 100644 --- a/silkworm/sentry/rlpx/framing/framing_cipher.cpp +++ b/silkworm/sentry/rlpx/framing/framing_cipher.cpp @@ -202,9 +202,7 @@ FramingCipher::FramingCipher(const KeyMaterial& key_material) { impl_ = std::make_unique(key_material, aes_secret, mac_secret); } -FramingCipher::~FramingCipher() { - assert(true); -} +FramingCipher::~FramingCipher() = default; FramingCipher::FramingCipher(FramingCipher&& other) noexcept : impl_(std::move(other.impl_)) {} diff --git a/silkworm/sentry/rlpx/framing/message_frame_codec.cpp b/silkworm/sentry/rlpx/framing/message_frame_codec.cpp index cf2e48e3ca..fbc9f7fdc8 100644 --- a/silkworm/sentry/rlpx/framing/message_frame_codec.cpp +++ b/silkworm/sentry/rlpx/framing/message_frame_codec.cpp @@ -33,7 +33,7 @@ static Bytes snappy_compress(ByteView data) { Bytes output; output.resize(snappy::MaxCompressedLength(data.size())); - size_t compressed_length; + size_t compressed_length{0}; snappy::RawCompress( reinterpret_cast(data.data()), data.size(), @@ -45,7 +45,7 @@ static Bytes snappy_compress(ByteView data) { } static size_t snappy_uncompressed_length(ByteView data) { - size_t uncompressed_length; + size_t uncompressed_length{0}; bool ok = snappy::GetUncompressedLength( reinterpret_cast(data.data()), data.size(), @@ -87,7 +87,7 @@ Message MessageFrameCodec::decode(ByteView frame_data) const { if (frame_data.empty()) throw std::runtime_error("MessageFrameCodec: frame size too short"); - uint8_t id; + uint8_t id{0}; auto id_data = ByteView{frame_data.substr(0, 1)}; success_or_throw(rlp::decode(id_data, id), "MessageFrameCodec: failed to decode a message ID"); diff --git a/silkworm/sentry/rlpx/peer.cpp b/silkworm/sentry/rlpx/peer.cpp index d5d8bd3fcd..0ef7a292d5 100644 --- a/silkworm/sentry/rlpx/peer.cpp +++ b/silkworm/sentry/rlpx/peer.cpp @@ -212,8 +212,8 @@ Task Peer::drop(const std::shared_ptr& peer, DisconnectReason reason return concurrency::co_spawn_sw(peer->strand_, Peer::drop_in_strand(peer, reason), use_awaitable); } -Task Peer::drop_in_strand(std::shared_ptr self, DisconnectReason reason) { - co_await self->drop(reason); +Task Peer::drop_in_strand(std::shared_ptr peer, DisconnectReason reason) { + co_await peer->drop(reason); } Task Peer::drop(DisconnectReason reason) { diff --git a/silkworm/sentry/sentry_client_factory.cpp b/silkworm/sentry/sentry_client_factory.cpp index 7de94fc9e1..96ddcd8c4a 100644 --- a/silkworm/sentry/sentry_client_factory.cpp +++ b/silkworm/sentry/sentry_client_factory.cpp @@ -23,7 +23,7 @@ namespace silkworm::sentry { SentryClientFactory::SentryPtrPair SentryClientFactory::make_sentry( Settings sentry_settings, - std::vector remote_sentry_addresses, + const std::vector& remote_sentry_addresses, concurrency::ExecutorPool& executor_pool, rpc::GrpcContextPool& grpc_context_pool, SessionSentryClient::StatusDataProvider eth_status_data_provider) { @@ -52,7 +52,7 @@ SentryClientFactory::SentryPtrPair SentryClientFactory::make_sentry( // Wrap remote client in a session client auto session_sentry_client = std::make_shared( remote_sentry_client, - std::move(eth_status_data_provider)); + eth_status_data_provider); clients.push_back(session_sentry_client); } diff --git a/silkworm/sentry/sentry_client_factory.hpp b/silkworm/sentry/sentry_client_factory.hpp index 0d92ba3c32..22374c6598 100644 --- a/silkworm/sentry/sentry_client_factory.hpp +++ b/silkworm/sentry/sentry_client_factory.hpp @@ -38,7 +38,7 @@ struct SentryClientFactory { static SentryPtrPair make_sentry( Settings sentry_settings, - std::vector remote_sentry_addresses, + const std::vector& remote_sentry_addresses, concurrency::ExecutorPool& executor_pool, rpc::GrpcContextPool& grpc_context_pool, SessionSentryClient::StatusDataProvider eth_status_data_provider); diff --git a/silkworm/sentry/session_sentry_client.cpp b/silkworm/sentry/session_sentry_client.cpp index 4f01b4058e..48a85ede19 100644 --- a/silkworm/sentry/session_sentry_client.cpp +++ b/silkworm/sentry/session_sentry_client.cpp @@ -152,7 +152,7 @@ class SessionSentryClientImpl : public api::SentryClient { } Task run_transitions_until_ready(Event event) { - State state; + State state{State::kInit}; std::optional ready_waiter; do { std::tie(state, ready_waiter) = proceed_to_next_state(event); @@ -181,7 +181,7 @@ class SessionSentryClientImpl : public api::SentryClient { SessionSentryClient::SessionSentryClient( std::shared_ptr sentry_client, StatusDataProvider status_data_provider) - : p_impl_(std::make_unique(sentry_client, status_data_provider)) { + : p_impl_(std::make_unique(std::move(sentry_client), std::move(status_data_provider))) { } SessionSentryClient::~SessionSentryClient() {