20#ifndef RIPPLE_OVERLAY_SLOT_H_INCLUDED
21#define RIPPLE_OVERLAY_SLOT_H_INCLUDED
23#include <xrpld/overlay/Peer.h>
24#include <xrpld/overlay/ReduceRelayCommon.h>
25#include <xrpld/overlay/Squelch.h>
26#include <xrpl/basics/Log.h>
27#include <xrpl/basics/chrono.h>
28#include <xrpl/beast/container/aged_unordered_map.h>
29#include <xrpl/beast/utility/Journal.h>
30#include <xrpl/protocol/PublicKey.h>
31#include <xrpl/protocol/messages.h>
43namespace reduce_relay {
45template <
typename clock_type>
60template <
typename Unit,
typename TP>
64 return std::chrono::duration_cast<Unit>(t.time_since_epoch());
103template <
typename clock_type>
107 friend class Slots<clock_type>;
188 unordered_map<id_t, std::tuple<PeerState, uint16_t, uint32_t, uint32_t>>
238template <
typename clock_type>
243 auto now = clock_type::now();
244 for (
auto it = peers_.begin(); it != peers_.end();)
246 auto& peer = it->second;
249 if (now - peer.lastMessage >
IDLED)
251 JLOG(journal_.trace())
252 <<
"deleteIdlePeer: " <<
Slice(validator) <<
" " <<
id
254 << duration_cast<seconds>(now - peer.lastMessage).count()
256 deletePeer(validator,
id,
false);
261template <
typename clock_type>
266 protocol::MessageType type)
269 auto now = clock_type::now();
270 auto it = peers_.find(
id);
272 if (it == peers_.end())
274 JLOG(journal_.trace())
275 <<
"update: adding peer " <<
Slice(validator) <<
" " << id;
284 JLOG(journal_.trace())
285 <<
"update: squelch expired " <<
Slice(validator) <<
" " << id;
287 it->second.lastMessage = now;
292 auto& peer = it->second;
294 JLOG(journal_.trace())
295 <<
"update: existing peer " <<
Slice(validator) <<
" " <<
id
296 <<
" slot state " <<
static_cast<int>(state_) <<
" peer state "
297 <<
static_cast<int>(peer.state) <<
" count " << peer.count <<
" last "
298 << duration_cast<milliseconds>(now - peer.lastMessage).count()
299 <<
" pool " << considered_.
size() <<
" threshold " << reachedThreshold_
300 <<
" " << (type == protocol::mtVALIDATION ?
"validation" :
"proposal");
302 peer.lastMessage = now;
308 considered_.insert(
id);
314 JLOG(journal_.trace())
315 <<
"update: resetting due to inactivity " <<
Slice(validator) <<
" "
316 <<
id <<
" " << duration_cast<seconds>(now - lastSelected_).count();
330 auto const consideredPoolSize = considered_.
size();
334 considered_.size() == 1 ? 0 :
rand_int(considered_.size() - 1);
335 auto it =
std::next(considered_.begin(), i);
337 considered_.erase(it);
338 auto const& itpeers = peers_.find(
id);
339 if (itpeers == peers_.end())
341 JLOG(journal_.error()) <<
"update: peer not found "
342 <<
Slice(validator) <<
" " << id;
345 if (now - itpeers->second.lastMessage <
IDLED)
351 JLOG(journal_.trace())
352 <<
"update: selection failed " <<
Slice(validator) <<
" " << id;
359 auto s = selected.
begin();
360 JLOG(journal_.trace())
361 <<
"update: " <<
Slice(validator) <<
" " <<
id <<
" pool size "
362 << consideredPoolSize <<
" selected " << *s <<
" "
367 "ripple::reduce_relay::Slot::update : minimum peers");
372 for (
auto& [k, v] : peers_)
376 if (selected.
find(k) != selected.
end())
380 if (journal_.trace())
389 JLOG(journal_.trace()) <<
"update: squelching " <<
Slice(validator)
390 <<
" " <<
id <<
" " << str.
str();
392 reachedThreshold_ = 0;
397template <
typename clock_type>
407 JLOG(journal_.warn())
408 <<
"getSquelchDuration: unexpected squelch duration " << npeers;
413template <
typename clock_type>
417 auto it = peers_.find(
id);
418 if (it != peers_.end())
420 JLOG(journal_.trace())
421 <<
"deletePeer: " <<
Slice(validator) <<
" " <<
id <<
" selected "
423 << (considered_.find(
id) != considered_.end()) <<
" erase "
425 auto now = clock_type::now();
428 for (
auto& [k, v] : peers_)
431 handler_.unsquelch(validator, k);
438 reachedThreshold_ = 0;
441 else if (considered_.find(
id) != considered_.end())
445 considered_.erase(
id);
448 it->second.lastMessage = now;
449 it->second.count = 0;
456template <
typename clock_type>
460 for (
auto& [_, peer] : peers_)
467template <
typename clock_type>
473 reachedThreshold_ = 0;
477template <
typename clock_type>
481 return std::count_if(peers_.begin(), peers_.end(), [&](
auto const& it) {
482 return (it.second.state == state);
486template <
typename clock_type>
490 return std::count_if(peers_.begin(), peers_.end(), [&](
auto const& it) {
491 return (it.second.state != state);
495template <
typename clock_type>
500 for (
auto const& [
id, info] : peers_)
506template <
typename clock_type>
517 for (
auto const& [
id, info] : peers_)
523 epoch<milliseconds>(info.expire).count(),
524 epoch<milliseconds>(info.lastMessage).count()))));
533template <
typename clock_type>
565 protocol::MessageType type);
577 auto const& it =
slots_.find(validator);
579 return it->second.inState(state);
587 auto const& it =
slots_.find(validator);
589 return it->second.notInState(state);
597 auto const& it =
slots_.find(validator);
599 return it->second.state_ == state;
607 auto const& it =
slots_.find(validator);
609 return it->second.getSelected();
621 auto const& it =
slots_.find(validator);
623 return it->second.getPeers();
631 auto const& it =
slots_.find(validator);
633 return it->second.getState();
662 beast::get_abstract_clock<clock_type>()};
665template <
typename clock_type>
673 auto it = peersWithMessage_.find(key);
674 if (it == peersWithMessage_.end())
676 JLOG(journal_.trace())
677 <<
"addPeerMessage: new " <<
to_string(key) <<
" " << id;
682 if (it->second.find(
id) != it->second.end())
684 JLOG(journal_.trace()) <<
"addPeerMessage: duplicate message "
689 JLOG(journal_.trace())
690 <<
"addPeerMessage: added " <<
to_string(key) <<
" " << id;
698template <
typename clock_type>
704 protocol::MessageType type)
706 if (!addPeerMessage(key,
id))
709 auto it = slots_.find(validator);
710 if (it == slots_.end())
712 JLOG(journal_.trace())
713 <<
"updateSlotAndSquelch: new slot " <<
Slice(validator);
719 it->second.update(validator,
id, type);
722 it->second.update(validator,
id, type);
725template <
typename clock_type>
729 for (
auto& [validator, slot] : slots_)
730 slot.deletePeer(validator,
id,
erase);
733template <
typename clock_type>
737 auto now = clock_type::now();
739 for (
auto it = slots_.begin(); it != slots_.end();)
741 it->second.deleteIdlePeer(it->first);
744 JLOG(journal_.trace())
745 <<
"deleteIdlePeers: deleting idle slot " <<
Slice(it->first);
746 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.
Slot(SquelchHandler const &handler, beast::Journal journal)
Constructor.
std::unordered_map< id_t, std::tuple< PeerState, uint16_t, uint32_t, uint32_t > > getPeers() const
Get peers info.
void deletePeer(PublicKey const &validator, id_t id, bool erase)
Handle peer deletion when a peer disconnects.
std::uint16_t reachedThreshold_
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.
void update(PublicKey const &validator, id_t id, protocol::MessageType type)
Update peer info.
clock_type::time_point lastSelected_
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.
const time_point & getLastSelected() const
Get the time of the last peer selection round.
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
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.
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.
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.
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)
Calls Slot::update of Slot associated with the validator.
Slots(Logs &logs, SquelchHandler const &handler)
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 uint16_t MAX_SELECTED_PEERS
static constexpr auto MIN_UNSQUELCH_EXPIRE
static constexpr auto IDLED
static constexpr uint16_t MAX_MESSAGE_THRESHOLD
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.