From 1f492aaa4017a3686da4117c5d97e403b52a75d9 Mon Sep 17 00:00:00 2001 From: battlmonstr Date: Mon, 4 Nov 2024 19:29:37 +0100 Subject: [PATCH] sync: remove singleton from core (#2474) --- silkworm/sync/block_exchange.cpp | 6 +++--- silkworm/sync/internals/body_sequence.cpp | 4 ++-- silkworm/sync/internals/header_chain.cpp | 4 ++-- .../internals/random_number.hpp} | 16 +++++----------- silkworm/sync/messages/inbound_new_block.cpp | 4 ++-- .../sync/messages/inbound_new_block_hashes.cpp | 4 ++-- 6 files changed, 16 insertions(+), 22 deletions(-) rename silkworm/{core/common/singleton.hpp => sync/internals/random_number.hpp} (69%) diff --git a/silkworm/sync/block_exchange.cpp b/silkworm/sync/block_exchange.cpp index 74f2a17fd7..a4c5e5028a 100644 --- a/silkworm/sync/block_exchange.cpp +++ b/silkworm/sync/block_exchange.cpp @@ -22,9 +22,9 @@ #include #include -#include #include #include +#include #include #include #include @@ -117,8 +117,8 @@ void BlockExchange::execution_loop() { size_t room_for_new_requests = peers_capacity > outstanding_requests ? peers_capacity - outstanding_requests : 0; auto body_requests = room_for_new_requests == 1 - ? Singleton::instance().generate_one() % 2 // 50% chance to request a body - : room_for_new_requests / 2; // a slight bias towards headers + ? chainsync::random_number.generate_one() % 2 // 50% chance to request a body + : room_for_new_requests / 2; // a slight bias towards headers room_for_new_requests -= request_bodies(now, body_requests); // do the computed nr. of body requests room_for_new_requests -= request_headers(now, room_for_new_requests); // do the remaining nr. of header requests diff --git a/silkworm/sync/internals/body_sequence.cpp b/silkworm/sync/internals/body_sequence.cpp index 25fd1cee67..83cdd2bf60 100644 --- a/silkworm/sync/internals/body_sequence.cpp +++ b/silkworm/sync/internals/body_sequence.cpp @@ -17,9 +17,9 @@ #include "body_sequence.hpp" #include -#include #include #include +#include #include namespace silkworm { @@ -144,7 +144,7 @@ std::shared_ptr BodySequence::request_bodies(time_point_t tp) { auto body_request = std::make_shared(); auto& packet = body_request->packet(); - packet.request_id = Singleton::instance().generate_one(); + packet.request_id = chainsync::random_number.generate_one(); auto penalizations = renew_stale_requests(packet, min_block, tp, timeout); diff --git a/silkworm/sync/internals/header_chain.cpp b/silkworm/sync/internals/header_chain.cpp index 226d7960f2..153f028c19 100644 --- a/silkworm/sync/internals/header_chain.cpp +++ b/silkworm/sync/internals/header_chain.cpp @@ -21,12 +21,12 @@ #include #include -#include #include #include #include #include #include +#include #include #include "algorithm.hpp" @@ -539,7 +539,7 @@ std::optional HeaderChain::save_external_announce(Hash if (has_link(hash)) return std::nullopt; // we already have this link, no need to request it GetBlockHeadersPacket66 request; - request.request_id = Singleton::instance().generate_one(); + request.request_id = chainsync::random_number.generate_one(); request.request.origin = {hash}; request.request.amount = 1; request.request.skip = 0; diff --git a/silkworm/core/common/singleton.hpp b/silkworm/sync/internals/random_number.hpp similarity index 69% rename from silkworm/core/common/singleton.hpp rename to silkworm/sync/internals/random_number.hpp index 1945f9d2ad..25eab6efb1 100644 --- a/silkworm/core/common/singleton.hpp +++ b/silkworm/sync/internals/random_number.hpp @@ -1,5 +1,5 @@ /* - Copyright 2022 The Silkworm Authors + Copyright 2024 The Silkworm Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -16,16 +16,10 @@ #pragma once -#include +#include -namespace silkworm { +namespace silkworm::chainsync { -template -class Singleton { - static inline T instance_; +inline RandomNumber random_number; - public: - static T& instance() { return instance_; } -}; - -} // namespace silkworm +} // namespace silkworm::chainsync diff --git a/silkworm/sync/messages/inbound_new_block.cpp b/silkworm/sync/messages/inbound_new_block.cpp index 3d8ec90e4e..3925905623 100644 --- a/silkworm/sync/messages/inbound_new_block.cpp +++ b/silkworm/sync/messages/inbound_new_block.cpp @@ -17,16 +17,16 @@ #include "inbound_new_block.hpp" #include -#include #include #include #include +#include namespace silkworm { InboundNewBlock::InboundNewBlock(ByteView data, PeerId peer_id) : peer_id_(std::move(peer_id)), - req_id_(Singleton::instance().generate_one()) // for trace purposes + req_id_(chainsync::random_number.generate_one()) // for trace purposes { success_or_throw(rlp::decode(data, packet_)); SILK_TRACE << "Received message " << *this; diff --git a/silkworm/sync/messages/inbound_new_block_hashes.cpp b/silkworm/sync/messages/inbound_new_block_hashes.cpp index 9332f0ff66..47a744b200 100644 --- a/silkworm/sync/messages/inbound_new_block_hashes.cpp +++ b/silkworm/sync/messages/inbound_new_block_hashes.cpp @@ -19,11 +19,11 @@ #include #include -#include #include #include #include #include +#include #include #include "outbound_get_block_headers.hpp" @@ -32,7 +32,7 @@ namespace silkworm { InboundNewBlockHashes::InboundNewBlockHashes(ByteView data, PeerId peer_id) : peer_id_(std::move(peer_id)), - req_id_(Singleton::instance().generate_one()) // for trace purposes + req_id_(chainsync::random_number.generate_one()) // for trace purposes { success_or_throw(rlp::decode(data, packet_)); SILK_TRACE << "Received message " << *this;