20#ifndef RIPPLE_OVERLAY_SLOT_H_INCLUDED
21#define RIPPLE_OVERLAY_SLOT_H_INCLUDED
23#include <xrpld/core/Config.h>
24#include <xrpld/overlay/Peer.h>
25#include <xrpld/overlay/ReduceRelayCommon.h>
27#include <xrpl/basics/Log.h>
28#include <xrpl/basics/chrono.h>
29#include <xrpl/basics/random.h>
30#include <xrpl/beast/container/aged_unordered_map.h>
31#include <xrpl/beast/utility/Journal.h>
32#include <xrpl/protocol/PublicKey.h>
33#include <xrpl/protocol/messages.h>
44namespace reduce_relay {
46template <
typename clock_type>
61template <
typename Unit,
typename TP>
65 return std::chrono::duration_cast<Unit>(t.time_since_epoch());
104template <
typename clock_type>
108 friend class Slots<clock_type>;
124 uint16_t maxSelectedPeers)
157 protocol::MessageType type,
262template <
typename clock_type>
267 auto now = clock_type::now();
268 for (
auto it = peers_.begin(); it != peers_.end();)
270 auto& peer = it->second;
273 if (now - peer.lastMessage >
IDLED)
275 JLOG(journal_.trace())
276 <<
"deleteIdlePeer: " <<
Slice(validator) <<
" " <<
id
278 << duration_cast<seconds>(now - peer.lastMessage).count()
280 deletePeer(validator,
id,
false);
285template <
typename clock_type>
290 protocol::MessageType type,
294 auto now = clock_type::now();
295 auto it = peers_.find(
id);
297 if (it == peers_.end())
299 JLOG(journal_.trace())
300 <<
"update: adding peer " <<
Slice(validator) <<
" " << id;
309 JLOG(journal_.trace())
310 <<
"update: squelch expired " <<
Slice(validator) <<
" " << id;
312 it->second.lastMessage = now;
317 auto& peer = it->second;
319 JLOG(journal_.trace())
320 <<
"update: existing peer " <<
Slice(validator) <<
" " <<
id
321 <<
" slot state " <<
static_cast<int>(state_) <<
" peer state "
322 <<
static_cast<int>(peer.state) <<
" count " << peer.count <<
" last "
323 << duration_cast<milliseconds>(now - peer.lastMessage).count()
324 <<
" pool " << considered_.
size() <<
" threshold " << reachedThreshold_
325 <<
" " << (type == protocol::mtVALIDATION ?
"validation" :
"proposal");
327 peer.lastMessage = now;
337 considered_.insert(
id);
343 JLOG(journal_.trace())
344 <<
"update: resetting due to inactivity " <<
Slice(validator) <<
" "
345 <<
id <<
" " << duration_cast<seconds>(now - lastSelected_).count();
350 if (reachedThreshold_ == maxSelectedPeers_)
359 auto const consideredPoolSize = considered_.
size();
360 while (selected.
size() != maxSelectedPeers_ && considered_.size() != 0)
363 considered_.size() == 1 ? 0 :
rand_int(considered_.size() - 1);
364 auto it =
std::next(considered_.begin(), i);
366 considered_.erase(it);
367 auto const& itpeers = peers_.find(
id);
368 if (itpeers == peers_.end())
370 JLOG(journal_.error()) <<
"update: peer not found "
371 <<
Slice(validator) <<
" " << id;
374 if (now - itpeers->second.lastMessage <
IDLED)
378 if (selected.
size() != maxSelectedPeers_)
380 JLOG(journal_.trace())
381 <<
"update: selection failed " <<
Slice(validator) <<
" " << id;
388 auto s = selected.
begin();
389 JLOG(journal_.trace())
390 <<
"update: " <<
Slice(validator) <<
" " <<
id <<
" pool size "
391 << consideredPoolSize <<
" selected " << *s <<
" "
395 peers_.size() >= maxSelectedPeers_,
396 "ripple::reduce_relay::Slot::update : minimum peers");
401 for (
auto& [k, v] : peers_)
405 if (selected.
find(k) != selected.
end())
409 if (journal_.trace())
413 getSquelchDuration(peers_.size() - maxSelectedPeers_);
418 JLOG(journal_.trace()) <<
"update: squelching " <<
Slice(validator)
419 <<
" " <<
id <<
" " << str.
str();
421 reachedThreshold_ = 0;
426template <
typename clock_type>
436 JLOG(journal_.warn())
437 <<
"getSquelchDuration: unexpected squelch duration " << npeers;
442template <
typename clock_type>
446 auto it = peers_.find(
id);
447 if (it != peers_.end())
451 JLOG(journal_.trace())
452 <<
"deletePeer: " <<
Slice(validator) <<
" " <<
id <<
" selected "
454 << (considered_.find(
id) != considered_.end()) <<
" erase "
456 auto now = clock_type::now();
459 for (
auto& [k, v] : peers_)
469 reachedThreshold_ = 0;
472 else if (considered_.find(
id) != considered_.end())
476 considered_.erase(
id);
479 it->second.lastMessage = now;
480 it->second.count = 0;
486 for (
auto const& k : toUnsquelch)
487 handler_.unsquelch(validator, k);
491template <
typename clock_type>
495 for (
auto& [_, peer] : peers_)
502template <
typename clock_type>
508 reachedThreshold_ = 0;
512template <
typename clock_type>
516 return std::count_if(peers_.begin(), peers_.end(), [&](
auto const& it) {
517 return (it.second.state == state);
521template <
typename clock_type>
525 return std::count_if(peers_.begin(), peers_.end(), [&](
auto const& it) {
526 return (it.second.state != state);
530template <
typename clock_type>
535 for (
auto const& [
id, info] : peers_)
541template <
typename clock_type>
552 for (
auto const& [
id, info] : peers_)
558 epoch<milliseconds>(info.expire).count(),
559 epoch<milliseconds>(info.lastMessage).count()))));
568template <
typename clock_type>
608 reduce_relay::epoch<std::chrono::minutes>(clock_type::now()) >
626 protocol::MessageType type)
643 protocol::MessageType type,
656 auto const& it =
slots_.find(validator);
658 return it->second.inState(state);
666 auto const& it =
slots_.find(validator);
668 return it->second.notInState(state);
676 auto const& it =
slots_.find(validator);
678 return it->second.state_ == state;
686 auto const& it =
slots_.find(validator);
688 return it->second.getSelected();
700 auto const& it =
slots_.find(validator);
702 return it->second.getPeers();
710 auto const& it =
slots_.find(validator);
712 return it->second.getState();
747 beast::get_abstract_clock<clock_type>()};
750template <
typename clock_type>
758 auto it = peersWithMessage_.find(key);
759 if (it == peersWithMessage_.end())
761 JLOG(journal_.trace())
762 <<
"addPeerMessage: new " <<
to_string(key) <<
" " << id;
767 if (it->second.find(
id) != it->second.end())
769 JLOG(journal_.trace()) <<
"addPeerMessage: duplicate message "
774 JLOG(journal_.trace())
775 <<
"addPeerMessage: added " <<
to_string(key) <<
" " << id;
783template <
typename clock_type>
789 protocol::MessageType type,
792 if (!addPeerMessage(key,
id))
795 auto it = slots_.find(validator);
796 if (it == slots_.end())
798 JLOG(journal_.trace())
799 <<
"updateSlotAndSquelch: new slot " <<
Slice(validator);
805 handler_, logs_.journal(
"Slot"), maxSelectedPeers_)))
807 it->second.update(validator,
id, type, callback);
810 it->second.update(validator,
id, type, callback);
813template <
typename clock_type>
817 for (
auto& [validator, slot] : slots_)
818 slot.deletePeer(validator,
id,
erase);
821template <
typename clock_type>
825 auto now = clock_type::now();
827 for (
auto it = slots_.begin(); it != slots_.end();)
829 it->second.deleteIdlePeer(it->first);
832 JLOG(journal_.trace())
833 <<
"deleteIdlePeers: deleting idle slot " <<
Slice(it->first);
834 it = slots_.erase(it);
A generic endpoint for log messages.
Associative container where each element is also indexed by time.
Manages partitions for logging.
std::uint32_t id_t
Uniquely identifies a peer.
An immutable linear range of bytes.
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Seed functor once per construction.
Slot is associated with a specific validator via validator's public key.
std::unordered_map< id_t, std::tuple< PeerState, uint16_t, uint32_t, uint32_t > > getPeers() const
Get peers info.
Slot(SquelchHandler const &handler, beast::Journal journal, uint16_t maxSelectedPeers)
Constructor.
void deletePeer(PublicKey const &validator, id_t id, bool erase)
Handle peer deletion when a peer disconnects.
std::uint16_t reachedThreshold_
uint16_t const maxSelectedPeers_
std::uint16_t notInState(PeerState state) const
Return number of peers not in state.
typename clock_type::time_point time_point
SquelchHandler const & handler_
void initCounting()
Initialize slot to Counting state.
time_point const & getLastSelected() const
Get the time of the last peer selection round.
clock_type::time_point lastSelected_
void update(PublicKey const &validator, id_t id, protocol::MessageType type, ignored_squelch_callback callback)
Update peer info.
std::set< id_t > getSelected() const
Return selected peers.
SlotState getState() const
Return Slot's state.
std::uint16_t inState(PeerState state) const
Return number of peers in state.
void deleteIdlePeer(PublicKey const &validator)
Check if peers stopped relaying messages.
std::unordered_set< id_t > considered_
std::chrono::seconds getSquelchDuration(std::size_t npeers)
Get random squelch duration between MIN_UNSQUELCH_EXPIRE and min(max(MAX_UNSQUELCH_EXPIRE_DEFAULT,...
std::unordered_map< id_t, PeerInfo > peers_
void resetCounts()
Reset counts of peers in Selected or Counting state.
beast::Journal const journal_
Slots is a container for validator's Slot and handles Slot update when a message is received from a v...
typename clock_type::time_point time_point
bool reduceRelayReady()
Check if reduce_relay::WAIT_ON_BOOTUP time passed since startup.
static messages peersWithMessage_
void deletePeer(id_t id, bool erase)
Called when a peer is deleted.
void deleteIdlePeers()
Check if peers stopped relaying messages and if slots stopped receiving messages from the validator.
bool addPeerMessage(uint256 const &key, id_t id)
Add message/peer if have not seen this message from the peer.
bool const baseSquelchEnabled_
beast::Journal const journal_
std::optional< std::uint16_t > notInState(PublicKey const &validator, PeerState state) const
Return number of peers not in state.
std::set< id_t > getSelected(PublicKey const &validator)
Get selected peers.
bool baseSquelchReady()
Check if base squelching feature is enabled and ready.
hash_map< PublicKey, Slot< clock_type > > slots_
std::optional< std::uint16_t > inState(PublicKey const &validator, PeerState state) const
Return number of peers in state.
bool inState(PublicKey const &validator, SlotState state) const
Return true if Slot is in state.
std::unordered_map< typename Peer::id_t, std::tuple< PeerState, uint16_t, uint32_t, std::uint32_t > > getPeers(PublicKey const &validator)
Get peers info.
uint16_t const maxSelectedPeers_
std::optional< SlotState > getState(PublicKey const &validator)
Get Slot's state.
void updateSlotAndSquelch(uint256 const &key, PublicKey const &validator, id_t id, protocol::MessageType type, typename Slot< clock_type >::ignored_squelch_callback callback)
Calls Slot::update of Slot associated with the validator.
std::atomic_bool reduceRelayReady_
void updateSlotAndSquelch(uint256 const &key, PublicKey const &validator, id_t id, protocol::MessageType type)
Calls Slot::update of Slot associated with the validator, with a noop callback.
Slots(Logs &logs, SquelchHandler const &handler, Config const &config)
SquelchHandler const & handler_
virtual void unsquelch(PublicKey const &validator, Peer::id_t id) const =0
Unsquelch handler.
virtual ~SquelchHandler()
virtual void squelch(PublicKey const &validator, Peer::id_t id, std::uint32_t duration) const =0
Squelch handler.
std::enable_if< is_aged_container< AgedContainer >::value, std::size_t >::type expire(AgedContainer &c, std::chrono::duration< Rep, Period > const &age)
Expire aged container items past the specified age.
static constexpr auto SQUELCH_PER_PEER
static constexpr auto MIN_UNSQUELCH_EXPIRE
static constexpr auto IDLED
static constexpr uint16_t MAX_MESSAGE_THRESHOLD
static constexpr auto WAIT_ON_BOOTUP
static constexpr auto MAX_UNSQUELCH_EXPIRE_DEFAULT
static constexpr uint16_t MIN_MESSAGE_THRESHOLD
static constexpr auto MAX_UNSQUELCH_EXPIRE_PEERS
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::enable_if_t< std::is_integral< Integral >::value, Integral > rand_int()
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
std::string to_string(base_uint< Bits, Tag > const &a)
Data maintained for each peer.