diff --git a/src/test/overlay/base_squelch_test.cpp b/src/test/overlay/base_squelch_test.cpp index b69bfb8324..17e58b11a6 100644 --- a/src/test/overlay/base_squelch_test.cpp +++ b/src/test/overlay/base_squelch_test.cpp @@ -701,10 +701,16 @@ private: } void - squelchAll(PublicKey const& validator, std::uint32_t duration) override + squelchAll( + PublicKey const& validator, + std::uint32_t duration, + std::function callback) override { for (auto const& [id, peer] : peers_) + { squelch_(validator, peer, duration); + callback(id); + } } void unsquelch(PublicKey const& validator, Peer::id_t id) const override @@ -1580,7 +1586,10 @@ vp_base_squelch_max_selected_peers=2 } void - squelchAll(PublicKey const&, std::uint32_t) override + squelchAll( + PublicKey const&, + std::uint32_t, + std::function) override { } diff --git a/src/test/overlay/enhanced_squelch_test.cpp b/src/test/overlay/enhanced_squelch_test.cpp index 5c6636103c..da69937c84 100644 --- a/src/test/overlay/enhanced_squelch_test.cpp +++ b/src/test/overlay/enhanced_squelch_test.cpp @@ -41,8 +41,8 @@ class TestHandler : public reduce_relay::SquelchHandler public: using squelch_method = std::function; - using squelchAll_method = - std::function; + using squelchAll_method = std::function< + void(PublicKey const&, std::uint32_t, std::function)>; using unsquelch_method = std::function; squelch_method squelch_f_; @@ -74,9 +74,12 @@ public: } void - squelchAll(PublicKey const& validator, std::uint32_t duration) override + squelchAll( + PublicKey const& validator, + std::uint32_t duration, + std::function callback) override { - squelchAll_f_(validator, duration); + squelchAll_f_(validator, duration, callback); } void @@ -148,10 +151,10 @@ public: BEAST_EXPECTS(false, "unexpected call to squelch handler"); }; - TestHandler::squelchAll_method noop_squelchAll = [&](PublicKey const&, - std::uint32_t) { - BEAST_EXPECTS(false, "unexpected call to squelchAll handler"); - }; + TestHandler::squelchAll_method noop_squelchAll = + [&](PublicKey const&, std::uint32_t, std::function) { + BEAST_EXPECTS(false, "unexpected call to squelchAll handler"); + }; TestHandler::unsquelch_method noop_unsquelch = [&](PublicKey const&, Peer::id_t) { @@ -369,10 +372,12 @@ 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_ = [&](PublicKey const& key, std::uint32_t) { + handler.squelchAll_f_ = [&](PublicKey const& key, + std::uint32_t, + std::function callback) { BEAST_EXPECTS( key == newValidator, "unexpected validator squelched"); - slots.squelchValidator(key, peerID); + callback(peerID); }; slots.updateUntrustedValidatorSlot( @@ -401,7 +406,8 @@ vp_enhanced_squelch_enable=0 // verify that squelchAll is called for each idled slot validator handler.squelchAll_f_ = [&](PublicKey const& actualKey, - std::uint32_t duration) { + std::uint32_t duration, + std::function callback) { for (auto it = keys.begin(); it != keys.end(); ++it) { if (*it == actualKey) @@ -727,10 +733,12 @@ vp_enhanced_squelch_enable=0 // verify that squelchAll is called for idle validator handler.squelchAll_f_ = [&](PublicKey const& actualKey, - std::uint32_t duration) { + std::uint32_t duration, + std::function callback) { BEAST_EXPECTS( actualKey == idleValidator, "unexpected key passed to squelchAll"); + callback(peerID); }; TestStopwatch stopwatch; diff --git a/src/xrpld/overlay/Slot.h b/src/xrpld/overlay/Slot.h index 6262923173..40315e8c3f 100644 --- a/src/xrpld/overlay/Slot.h +++ b/src/xrpld/overlay/Slot.h @@ -31,6 +31,7 @@ #include #include +#include #include namespace ripple { @@ -101,9 +102,13 @@ public: * to register that a (validator,peer) was squelched * @param validator Public key of the source validator * @param duration Squelch duration in seconds + * @param callback a callback to register that a validator was squelched */ virtual void - squelchAll(PublicKey const& validator, std::uint32_t duration) = 0; + squelchAll( + PublicKey const& validator, + std::uint32_t duration, + std::function callback) = 0; /** Unsquelch handler * @param validator Public key of the source validator @@ -426,17 +431,6 @@ public: void deletePeer(Peer::id_t id, bool erase); - /** Called to register that a given validator was squelched for a given - * peer. It is expected that this method is called by SquelchHandler. - * - * @param validatorKey Validator public key - * @param peerID peer ID - */ - void - registerSquelchedValidator( - PublicKey const& validatorKey, - Peer::id_t peerID); - void onWrite(beast::PropertyStream::Map& stream) const; @@ -480,6 +474,17 @@ protected: bool expireAndIsPeerSquelched(PublicKey const& validatorKey, Peer::id_t peerID); + /** Called to register that a given validator was squelched for a given + * peer. It is expected that this method is called by SquelchHandler. + * + * @param validatorKey Validator public key + * @param peerID peer ID + */ + void + registerSquelchedValidator( + PublicKey const& validatorKey, + Peer::id_t peerID); + std::atomic_bool reduceRelayReady_{false}; // Maintain an open number of slots for trusted validators to reduce diff --git a/src/xrpld/overlay/detail/OverlayImpl.cpp b/src/xrpld/overlay/detail/OverlayImpl.cpp index 05bfaedcd8..f9631ff83d 100644 --- a/src/xrpld/overlay/detail/OverlayImpl.cpp +++ b/src/xrpld/overlay/detail/OverlayImpl.cpp @@ -43,6 +43,8 @@ #include "xrpld/overlay/detail/TrafficCount.h" +#include + namespace ripple { namespace CrawlOptions { @@ -1418,11 +1420,14 @@ OverlayImpl::squelch( } void -OverlayImpl::squelchAll(PublicKey const& validator, uint32_t squelchDuration) +OverlayImpl::squelchAll( + PublicKey const& validator, + uint32_t squelchDuration, + std::function callback) { for_each([&](std::shared_ptr&& p) { p->send(makeSquelchMessage(validator, true, squelchDuration)); - slots_.registerSquelchedValidator(validator, p->id()); + callback(p->id()); }); } diff --git a/src/xrpld/overlay/detail/OverlayImpl.h b/src/xrpld/overlay/detail/OverlayImpl.h index 863451585e..8bcb45be4a 100644 --- a/src/xrpld/overlay/detail/OverlayImpl.h +++ b/src/xrpld/overlay/detail/OverlayImpl.h @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -467,8 +468,10 @@ private: std::uint32_t squelchDuration) const override; void - squelchAll(PublicKey const& validator, std::uint32_t squelchDuration) - override; + squelchAll( + PublicKey const& validator, + std::uint32_t squelchDuration, + std::function) override; void unsquelch(PublicKey const& validator, Peer::id_t id) const override; diff --git a/src/xrpld/overlay/detail/Slot.cpp b/src/xrpld/overlay/detail/Slot.cpp index 09ead75fc5..4fe7f62e8b 100644 --- a/src/xrpld/overlay/detail/Slot.cpp +++ b/src/xrpld/overlay/detail/Slot.cpp @@ -481,7 +481,10 @@ Slots::updateUntrustedValidatorSlot( // will be eventually cleaned and squelched if (untrustedSlots_.size() == MAX_UNTRUSTED_SLOTS) { - handler_.squelchAll(validator, MAX_UNSQUELCH_EXPIRE_DEFAULT.count()); + handler_.squelchAll( + validator, + MAX_UNSQUELCH_EXPIRE_DEFAULT.count(), + [&](Peer::id_t id) { registerSquelchedValidator(validator, id); }); return; } @@ -570,7 +573,11 @@ Slots::deleteIdlePeers() // sending messages for this validator squelch it if (!it->second.isTrusted_) handler_.squelchAll( - it->first, MAX_UNSQUELCH_EXPIRE_DEFAULT.count()); + it->first, + MAX_UNSQUELCH_EXPIRE_DEFAULT.count(), + [&](Peer::id_t id) { + registerSquelchedValidator(it->first, id); + }); it = slots.erase(it); } @@ -586,7 +593,10 @@ Slots::deleteIdlePeers() // there might be some good validators in this set that "lapsed". // However, since these are untrusted validators we're not concerned for (auto const& validator : cleanConsideredValidators()) - handler_.squelchAll(validator, MAX_UNSQUELCH_EXPIRE_DEFAULT.count()); + handler_.squelchAll( + validator, + MAX_UNSQUELCH_EXPIRE_DEFAULT.count(), + [&](Peer::id_t id) { registerSquelchedValidator(validator, id); }); } std::vector