diff --git a/src/test/overlay/base_squelch_test.cpp b/src/test/overlay/base_squelch_test.cpp index 61fbd8f46c..b31568be04 100644 --- a/src/test/overlay/base_squelch_test.cpp +++ b/src/test/overlay/base_squelch_test.cpp @@ -165,8 +165,8 @@ public: [[nodiscard]] uint256 const& getClosedLedgerHash() const override { - static uint256 const hash{}; - return hash; + static uint256 const kHash{}; + return kHash; } [[nodiscard]] bool hasLedger(uint256 const& hash, std::uint32_t seq) const override @@ -313,7 +313,7 @@ class Validator using Links = std::unordered_map; public: - Validator() : pkey_(std::get<0>(randomKeyPair(KeyType::Ed25519))), id_(sid_++) + Validator() : pkey_(std::get<0>(randomKeyPair(KeyType::Ed25519))), id_(sid++) { protocol::TMValidation v; v.set_validation("validation"); @@ -339,7 +339,7 @@ public: static void resetId() { - sid_ = 0; + sid = 0; } PublicKey const& @@ -370,7 +370,7 @@ public: { for (auto id : peers) { - assert(links_.find(id) != links_.end()); + assert(links_.contains(id)); f(*links_[id], message_); } } @@ -440,7 +440,7 @@ private: Links links_; PublicKey pkey_; MessageSPtr message_ = nullptr; - inline static std::uint16_t sid_ = 0; + inline static std::uint16_t sid = 0; std::uint16_t id_ = 0; }; @@ -469,7 +469,7 @@ class PeerSim : public PeerPartial, public std::enable_shared_from_this { public: PeerSim(Overlay& overlay, beast::Journal journal) - : id_(sid_++), overlay_(overlay), squelchStore_(journal, overlay_.clock()) + : overlay_(overlay), squelchStore_(journal, overlay_.clock()) { } @@ -484,7 +484,7 @@ public: static void resetId() { - sid_ = 0; + sid = 0; } /** @@ -514,8 +514,8 @@ public: } private: - inline static Peer::id_t sid_ = 0; - Peer::id_t id_; + inline static Peer::id_t sid = 0; + Peer::id_t id_{sid++}; Overlay& overlay_; reduce_relay::SquelchStore squelchStore_; }; @@ -881,12 +881,8 @@ public: bool isSelected(Peer::id_t id) { - for (auto& v : validators_) - { - if (overlay_.isSelected(v, id)) - return true; - } - return false; + return std::ranges::any_of( + validators_, [&](auto& v) { return overlay_.isSelected(v, id); }); } /** @@ -954,8 +950,8 @@ protected: return sp->id(); } - enum State { On, Off, WaitReset }; - enum EventType { LinkDown = 0, PeerDisconnected = 1 }; + enum class State { On, Off, WaitReset }; + enum class EventType { LinkDown = 0, PeerDisconnected = 1 }; // Link down or Peer disconnect event // TBD - add new peer event // TBD - add overlapping type of events at any @@ -965,8 +961,8 @@ protected: State state = State::Off; std::uint32_t cnt = 0; std::uint32_t handledCnt = 0; - bool isSelected_ = false; - Peer::id_t peer_{}; + bool isSelected = false; + Peer::id_t peer{}; std::uint16_t validator{}; std::optional key; TestStopwatch::time_point time; @@ -980,7 +976,8 @@ protected: void random(bool log) { - std::unordered_map events{{LinkDown, {}}, {PeerDisconnected, {}}}; + std::unordered_map events{ + {EventType::LinkDown, {}}, {EventType::PeerDisconnected, {}}}; auto lastCheck = network_.overlay().clock().now(); network_.reset(); @@ -1028,19 +1025,19 @@ protected: events[event].cnt++; events[event].validator = validator.id(); events[event].key = validator; - events[event].peer_ = link.peerId(); + events[event].peer = link.peerId(); events[event].state = State::On; events[event].time = now; if (event == EventType::LinkDown) { network_.enableLink(validator.id(), link.peerId(), false); - events[event].isSelected_ = + events[event].isSelected = network_.overlay().isSelected(validator, link.peerId()); } else { { - events[event].isSelected_ = network_.isSelected(link.peerId()); + events[event].isSelected = network_.isSelected(link.peerId()); } } }; @@ -1054,10 +1051,10 @@ protected: if (events[EventType::PeerDisconnected].state == State::On) { auto& event = events[EventType::PeerDisconnected]; - bool const allCounting = network_.allCounting(event.peer_); + bool const allCounting = network_.allCounting(event.peer); network_.overlay().deletePeer( - event.peer_, [&](PublicKey const& v, PeerWPtr const& peerPtr) { - if (event.isSelected_) + event.peer, [&](PublicKey const& v, PeerWPtr const& peerPtr) { + if (event.isSelected) sendSquelch(v, peerPtr, {}); event.handled = true; }); @@ -1066,14 +1063,14 @@ protected: // take place because there is no peers in Squelched state in // any of the slots where the peer is in Selected state // (allCounting is true) - bool const handled = (!event.isSelected_ && !event.handled) || - (event.isSelected_ && (event.handled || allCounting)); + bool const handled = (!event.isSelected && !event.handled) || + (event.isSelected && (event.handled || allCounting)); BEAST_EXPECT(handled); event.state = State::Off; - event.isSelected_ = false; + event.isSelected = false; event.handledCnt += handled; event.handled = false; - network_.onDisconnectPeer(event.peer_); + network_.onDisconnectPeer(event.peer); } auto& event = events[EventType::LinkDown]; @@ -1091,21 +1088,21 @@ protected: bool mustHandle = false; if (event.state == State::On && BEAST_EXPECT(event.key)) { - event.isSelected_ = network_.overlay().isSelected(*event.key, event.peer_); + event.isSelected = network_.overlay().isSelected(*event.key, event.peer); auto peers = network_.overlay().getPeers(*event.key); auto d = std::chrono::duration_cast( now.time_since_epoch()) .count() - std::chrono::duration_cast( - peers[event.peer_].lastMessage.time_since_epoch()) + peers[event.peer].lastMessage.time_since_epoch()) .count(); - mustHandle = event.isSelected_ && + mustHandle = event.isSelected && d > milliseconds(reduce_relay::kIdled).count() && network_.overlay().inState(*event.key, reduce_relay::PeerState::Squelched) > 0 && - peers.contains(event.peer_); + peers.contains(event.peer); } network_.overlay().deleteIdlePeers([&](PublicKey const& v, PeerWPtr const& ptr) { event.handled = true; @@ -1126,10 +1123,10 @@ protected: bool const handled = event.state == State::WaitReset || !event.handled; BEAST_EXPECT(handled); event.state = State::Off; - event.isSelected_ = false; + event.isSelected = false; event.handledCnt += handled; event.handled = false; - network_.enableLink(event.validator, event.peer_, true); + network_.enableLink(event.validator, event.peer, true); } }); @@ -1341,10 +1338,10 @@ protected: doTest("Test Config - squelch enabled (legacy)", log, [&](bool log) { Config c; - std::string const toLoad(R"rippleConfig( + std::string const toLoad(R"xrpldConfig( [reduce_relay] vp_enable=1 -)rippleConfig"); +)xrpldConfig"); c.loadFromString(toLoad); BEAST_EXPECT(c.vpReduceRelayBaseSquelchEnable == true); @@ -1353,19 +1350,19 @@ vp_enable=1 doTest("Test Config - squelch disabled (legacy)", log, [&](bool log) { Config c; - std::string toLoad(R"rippleConfig( + std::string toLoad(R"xrpldConfig( [reduce_relay] vp_enable=0 -)rippleConfig"); +)xrpldConfig"); c.loadFromString(toLoad); BEAST_EXPECT(c.vpReduceRelayBaseSquelchEnable == false); Config c1; - toLoad = R"rippleConfig( + toLoad = R"xrpldConfig( [reduce_relay] -)rippleConfig"; +)xrpldConfig"; c1.loadFromString(toLoad); BEAST_EXPECT(c1.vpReduceRelayBaseSquelchEnable == false); @@ -1374,10 +1371,10 @@ vp_enable=0 doTest("Test Config - squelch enabled", log, [&](bool log) { Config c; - std::string const toLoad(R"rippleConfig( + std::string const toLoad(R"xrpldConfig( [reduce_relay] vp_base_squelch_enable=1 -)rippleConfig"); +)xrpldConfig"); c.loadFromString(toLoad); BEAST_EXPECT(c.vpReduceRelayBaseSquelchEnable == true); @@ -1386,10 +1383,10 @@ vp_base_squelch_enable=1 doTest("Test Config - squelch disabled", log, [&](bool log) { Config c; - std::string const toLoad(R"rippleConfig( + std::string const toLoad(R"xrpldConfig( [reduce_relay] vp_base_squelch_enable=0 -)rippleConfig"); +)xrpldConfig"); c.loadFromString(toLoad); BEAST_EXPECT(c.vpReduceRelayBaseSquelchEnable == false); @@ -1398,11 +1395,11 @@ vp_base_squelch_enable=0 doTest("Test Config - legacy and new", log, [&](bool log) { Config c; - std::string const toLoad(R"rippleConfig( + std::string const toLoad(R"xrpldConfig( [reduce_relay] vp_base_squelch_enable=0 vp_enable=0 -)rippleConfig"); +)xrpldConfig"); std::string error; auto const expectedError = @@ -1427,29 +1424,29 @@ vp_enable=0 doTest("Test Config - max selected peers", log, [&](bool log) { Config c; - std::string toLoad(R"rippleConfig( + std::string toLoad(R"xrpldConfig( [reduce_relay] -)rippleConfig"); +)xrpldConfig"); c.loadFromString(toLoad); BEAST_EXPECT(c.vpReduceRelaySquelchMaxSelectedPeers == 5); Config c1; - toLoad = R"rippleConfig( + toLoad = R"xrpldConfig( [reduce_relay] vp_base_squelch_max_selected_peers=6 -)rippleConfig"; +)xrpldConfig"; c1.loadFromString(toLoad); BEAST_EXPECT(c1.vpReduceRelaySquelchMaxSelectedPeers == 6); Config c2; - toLoad = R"rippleConfig( + toLoad = R"xrpldConfig( [reduce_relay] vp_base_squelch_max_selected_peers=2 -)rippleConfig"; +)xrpldConfig"; std::string error; auto const expectedError = @@ -1536,13 +1533,11 @@ vp_base_squelch_max_selected_peers=2 struct Handler : public reduce_relay::SquelchHandler { - Handler() - { - } + Handler() = default; void squelch(PublicKey const&, Peer::id_t, std::uint32_t duration) const override { - maxDuration_ = std::max(duration, maxDuration_); + maxDuration = std::max(duration, maxDuration); } void @@ -1554,7 +1549,7 @@ vp_base_squelch_max_selected_peers=2 unsquelch(PublicKey const&, Peer::id_t) const override { } - mutable int maxDuration_{0}; + mutable int maxDuration{0}; }; void @@ -1565,7 +1560,7 @@ vp_base_squelch_max_selected_peers=2 Handler handler; auto run = [&](int npeers) { - handler.maxDuration_ = 0; + handler.maxDuration = 0; reduce_relay::Slots slots( env_.app(), handler, env_.app().config(), network_.overlay().clock()); // 1st message from a new peer switches the slot @@ -1592,12 +1587,12 @@ vp_base_squelch_max_selected_peers=2 // less than or equal to 60 peers run(20); BEAST_EXPECT( - handler.maxDuration_ >= kMinUnsquelchExpire.count() && - handler.maxDuration_ <= kMaxUnsquelchExpireDefault.count()); + handler.maxDuration >= kMinUnsquelchExpire.count() && + handler.maxDuration <= kMaxUnsquelchExpireDefault.count()); run(60); BEAST_EXPECT( - handler.maxDuration_ >= kMinUnsquelchExpire.count() && - handler.maxDuration_ <= kMaxUnsquelchExpireDefault.count()); + handler.maxDuration >= kMinUnsquelchExpire.count() && + handler.maxDuration <= kMaxUnsquelchExpireDefault.count()); // expect max duration greater than kMinUnsquelchExpire and less // than kMaxUnsquelchExpirePeers with peers greater than 60 // and less than 360 @@ -1606,10 +1601,10 @@ vp_base_squelch_max_selected_peers=2 // duration is probabilistic and max condition may still fail. // log when the value is low BEAST_EXPECT( - handler.maxDuration_ >= kMinUnsquelchExpire.count() && - handler.maxDuration_ <= kMaxUnsquelchExpirePeers.count()); + handler.maxDuration >= kMinUnsquelchExpire.count() && + handler.maxDuration <= kMaxUnsquelchExpirePeers.count()); using namespace beast::unit_test::detail; - if (handler.maxDuration_ <= kMaxUnsquelchExpireDefault.count()) + if (handler.maxDuration <= kMaxUnsquelchExpireDefault.count()) { log << makeReason("warning: squelch duration is low", __FILE__, __LINE__) << std::endl @@ -1618,9 +1613,9 @@ vp_base_squelch_max_selected_peers=2 // more than 400 is still less than kMaxUnsquelchExpirePeers run(400); BEAST_EXPECT( - handler.maxDuration_ >= kMinUnsquelchExpire.count() && - handler.maxDuration_ <= kMaxUnsquelchExpirePeers.count()); - if (handler.maxDuration_ <= kMaxUnsquelchExpireDefault.count()) + handler.maxDuration >= kMinUnsquelchExpire.count() && + handler.maxDuration <= kMaxUnsquelchExpirePeers.count()); + if (handler.maxDuration <= kMaxUnsquelchExpireDefault.count()) { log << makeReason("warning: squelch duration is low", __FILE__, __LINE__) << std::endl diff --git a/src/test/overlay/enhanced_squelch_test.cpp b/src/test/overlay/enhanced_squelch_test.cpp index fb38faaf1c..d0f2b2433d 100644 --- a/src/test/overlay/enhanced_squelch_test.cpp +++ b/src/test/overlay/enhanced_squelch_test.cpp @@ -33,28 +33,26 @@ public: std::function)>; using unsquelch_method = std::function; - squelch_method squelch_f_; - squelchAll_method squelchAll_f_; - unsquelch_method unsquelch_f_; + squelch_method squelchF; + squelchAll_method squelchAllF; + unsquelch_method unsquelchF; TestHandler(squelch_method squelchF, squelchAll_method squelchAllF, unsquelch_method unsquelchF) - : squelch_f_(std::move(squelchF)) - , squelchAll_f_(std::move(squelchAllF)) - , unsquelch_f_(std::move(unsquelchF)) + : squelchF(std::move(squelchF)) + , squelchAllF(std::move(squelchAllF)) + , unsquelchF(std::move(unsquelchF)) { } TestHandler(TestHandler& copy) - : squelch_f_(copy.squelch_f_) - , squelchAll_f_(copy.squelchAll_f_) - , unsquelch_f_(copy.unsquelch_f_) + : squelchF(copy.squelchF), squelchAllF(copy.squelchAllF), unsquelchF(copy.unsquelchF) { } void squelch(PublicKey const& validator, Peer::id_t peer, std::uint32_t duration) const override { - squelch_f_(validator, peer, duration); + squelchF(validator, peer, duration); } void @@ -63,13 +61,13 @@ public: std::uint32_t duration, std::function callback) override { - squelchAll_f_(validator, duration, callback); + squelchAllF(validator, duration, callback); } void unsquelch(PublicKey const& validator, Peer::id_t peer) const override { - unsquelch_f_(validator, peer); + unsquelchF(validator, peer); } }; @@ -102,11 +100,7 @@ public: return consideredValidators_; } - std::optional - updateConsideredValidator(PublicKey const& validator, Peer::id_t peerID) - { - return Slots::updateConsideredValidator(validator, peerID); - } + using Slots::updateConsideredValidator; void squelchValidator(PublicKey const& validatorKey, Peer::id_t peerID) @@ -163,25 +157,25 @@ public: testcase("Test Config - enabled enhanced squelching"); Config c; - std::string toLoad(R"rippleConfig( + std::string toLoad(R"xrpldConfig( [reduce_relay] vp_enhanced_squelch_enable=1 -)rippleConfig"); +)xrpldConfig"); c.loadFromString(toLoad); BEAST_EXPECT(c.vpReduceRelayEnhancedSquelchEnable == true); - toLoad = R"rippleConfig( + toLoad = R"xrpldConfig( [reduce_relay] vp_enhanced_squelch_enable=0 -)rippleConfig"; +)xrpldConfig"; c.loadFromString(toLoad); BEAST_EXPECT(c.vpReduceRelayEnhancedSquelchEnable == false); - toLoad = R"rippleConfig( + toLoad = R"xrpldConfig( [reduce_relay] -)rippleConfig"; +)xrpldConfig"; c.loadFromString(toLoad); BEAST_EXPECT(c.vpReduceRelayEnhancedSquelchEnable == false); @@ -328,7 +322,7 @@ vp_enhanced_squelch_enable=0 auto const newValidator = randomKeyPair(KeyType::Ed25519).first; // once slots are full squelchAll must be called for new peer/validator - handler.squelchAll_f_ = + handler.squelchAllF = [&](PublicKey const& key, std::uint32_t, std::function callback) { BEAST_EXPECTS(key == newValidator, "unexpected validator squelched"); callback(peerID); @@ -355,9 +349,9 @@ vp_enhanced_squelch_enable=0 auto keys = fillUntrustedSlots(slots); // verify that squelchAll is called for each idled slot validator - handler.squelchAll_f_ = [&](PublicKey const& actualKey, - std::uint32_t duration, - std::function callback) { + handler.squelchAllF = [&](PublicKey const& actualKey, + std::uint32_t duration, + std::function callback) { for (auto it = keys.begin(); it != keys.end(); ++it) { if (*it == actualKey) @@ -424,7 +418,7 @@ vp_enhanced_squelch_enable=0 testcase("updateUntrsutedValidatorSlot"); TestHandler handler{noopHandler}; - handler.squelch_f_ = [](PublicKey const&, Peer::id_t, std::uint32_t) {}; + handler.squelchF = [](PublicKey const&, Peer::id_t, std::uint32_t) {}; TestStopwatch stopwatch; EnhancedSquelchingTestSlots slots(env.app(), handler, env.app().config(), stopwatch); @@ -433,7 +427,7 @@ vp_enhanced_squelch_enable=0 std::vector peers = {}; // prepare n+1 peers, we expect the n+1st peer will be squelched - peers.reserve(env_.app().config().vpReduceRelaySquelchMaxSelectedPeers + 1); + peers.reserve(env.app().config().vpReduceRelaySquelchMaxSelectedPeers + 1); for (int i = 0; i < env.app().config().vpReduceRelaySquelchMaxSelectedPeers + 1; ++i) peers.push_back(i); @@ -674,9 +668,9 @@ vp_enhanced_squelch_enable=0 TestHandler handler{noopHandler}; // verify that squelchAll is called for poorly connected validator - handler.squelchAll_f_ = [&](PublicKey const& actualKey, - std::uint32_t duration, - std::function callback) { + handler.squelchAllF = [&](PublicKey const& actualKey, + std::uint32_t duration, + std::function callback) { BEAST_EXPECTS(actualKey == validator, "unexpected key passed to squelchAll"); callback(peerID); }; @@ -714,9 +708,9 @@ vp_enhanced_squelch_enable=0 TestHandler handler{noopHandler}; // verify that squelchAll is called for idle validator - handler.squelchAll_f_ = [&](PublicKey const& actualKey, - std::uint32_t duration, - std::function callback) { + handler.squelchAllF = [&](PublicKey const& actualKey, + std::uint32_t duration, + std::function callback) { BEAST_EXPECTS(actualKey == idleValidator, "unexpected key passed to squelchAll"); callback(peerID); }; @@ -758,9 +752,9 @@ vp_enhanced_squelch_enable=0 TestHandler handler{noopHandler}; // verify that squelchAll is called for idle validator - handler.squelchAll_f_ = [&](PublicKey const& actualKey, - std::uint32_t duration, - std::function callback) { + handler.squelchAllF = [&](PublicKey const& actualKey, + std::uint32_t duration, + std::function callback) { BEAST_EXPECTS(actualKey == validator, "unexpected key passed to squelchAll"); }; @@ -794,9 +788,9 @@ vp_enhanced_squelch_enable=0 auto const validators = fillUntrustedSlots(slots, 1); // verify that squelchAll is called for idle validator - handler.squelchAll_f_ = [&](PublicKey const& actualKey, - std::uint32_t duration, - std::function callback) { + handler.squelchAllF = [&](PublicKey const& actualKey, + std::uint32_t duration, + std::function callback) { BEAST_EXPECTS(actualKey == validators[0], "unexpected key passed to squelchAll"); }; diff --git a/src/xrpld/overlay/Slot.h b/src/xrpld/overlay/Slot.h index ec533cf615..3877d75786 100644 --- a/src/xrpld/overlay/Slot.h +++ b/src/xrpld/overlay/Slot.h @@ -147,11 +147,11 @@ public: */ struct PeerInfo { - PeerState state; // peer's state - std::size_t count{}; // message count - time_point expire; // squelch expiration time - time_point lastMessage; // time last message received - std::size_t timesSelected{}; // number of times the peer was selected + PeerState state{PeerState::Counting}; // peer's state + std::size_t count{}; // message count + time_point expire; // squelch expiration time + time_point lastMessage; // time last message received + std::size_t timesSelected{}; // number of times the peer was selected }; /** diff --git a/src/xrpld/overlay/detail/OverlayImpl.cpp b/src/xrpld/overlay/detail/OverlayImpl.cpp index af72006228..1b169fc617 100644 --- a/src/xrpld/overlay/detail/OverlayImpl.cpp +++ b/src/xrpld/overlay/detail/OverlayImpl.cpp @@ -1439,7 +1439,7 @@ OverlayImpl::squelchAll( std::uint32_t squelchDuration, std::function report) { - forEach([&](std::shared_ptr&& p) { + forEach([&](std::shared_ptr const& p) { p->send(makeSquelchMessage(validator, true, squelchDuration)); report(p->id()); }); @@ -1552,7 +1552,7 @@ OverlayImpl::handleUntrustedSquelch(PublicKey const& validator) // to acquire another lock on peers. Instead, count the number of peers in // the same loop, as we're already iterating all peers. auto total = 0; - forEach([&](std::shared_ptr&& p) { + forEach([&](std::shared_ptr const& p) { ++total; if (p->isSquelched(validator)) ++count; diff --git a/src/xrpld/overlay/detail/Slot.cpp b/src/xrpld/overlay/detail/Slot.cpp index d2a78519d4..dbb67f3606 100644 --- a/src/xrpld/overlay/detail/Slot.cpp +++ b/src/xrpld/overlay/detail/Slot.cpp @@ -493,7 +493,7 @@ Slots::updateConsideredValidator(PublicKey const& validator, Peer::id_t peer) if (it->second.count < reduce_relay::kMaxMessageThreshold) return std::nullopt; - auto const key = it->first; + auto key = it->first; consideredValidators_.erase(it); return key;