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>
26#include <xrpl/basics/Log.h>
27#include <xrpl/basics/chrono.h>
28#include <xrpl/basics/random.h>
29#include <xrpl/beast/container/aged_unordered_map.h>
30#include <xrpl/beast/utility/Journal.h>
31#include <xrpl/protocol/PublicKey.h>
32#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>;
189 unordered_map<id_t, std::tuple<PeerState, uint16_t, uint32_t, uint32_t>>
239template <
typename clock_type>
244 auto now = clock_type::now();
245 for (
auto it = peers_.begin(); it != peers_.end();)
247 auto& peer = it->second;
250 if (now - peer.lastMessage >
IDLED)
252 JLOG(journal_.trace())
255 << duration_cast<seconds>(now - peer.lastMessage).count()
262template <
typename clock_type>
267 protocol::MessageType type)
270 auto now = clock_type::now();
271 auto it = peers_.find(
id);
273 if (it == peers_.end())
275 JLOG(journal_.trace())
285 JLOG(journal_.trace())
288 it->second.lastMessage = now;
293 auto& peer = it->second;
295 JLOG(journal_.trace())
297 <<
" slot state " <<
static_cast<int>(state_) <<
" peer state "
298 <<
static_cast<int>(peer.state) <<
" count " << peer.count <<
" last "
299 << duration_cast<milliseconds>(now - peer.lastMessage).count()
300 <<
" pool " << considered_.
size() <<
" threshold " << reachedThreshold_
301 <<
" " << (type == protocol::mtVALIDATION ?
"validation" :
"proposal");
303 peer.lastMessage = now;
309 considered_.insert(
id);
315 JLOG(journal_.trace())
316 <<
"update: resetting due to inactivity " <<
Slice(
validator) <<
" "
317 <<
id <<
" " << duration_cast<seconds>(now - lastSelected_).count();
331 auto const consideredPoolSize = considered_.
size();
335 considered_.size() == 1 ? 0 :
rand_int(considered_.size() - 1);
336 auto it =
std::next(considered_.begin(), i);
338 considered_.erase(it);
339 auto const& itpeers = peers_.find(
id);
340 if (itpeers == peers_.end())
342 JLOG(journal_.error()) <<
"update: peer not found "
346 if (now - itpeers->second.lastMessage <
IDLED)
352 JLOG(journal_.trace())
360 auto s = selected.
begin();
361 JLOG(journal_.trace())
363 << consideredPoolSize <<
" selected " << *s <<
" "
368 "ripple::reduce_relay::Slot::update : minimum peers");
373 for (
auto& [k, v] : peers_)
377 if (selected.
find(k) != selected.
end())
381 if (journal_.trace())
390 JLOG(journal_.trace()) <<
"update: squelching " <<
Slice(
validator)
391 <<
" " <<
id <<
" " << str.
str();
393 reachedThreshold_ = 0;
398template <
typename clock_type>
408 JLOG(journal_.warn())
409 <<
"getSquelchDuration: unexpected squelch duration " << npeers;
414template <
typename clock_type>
418 auto it = peers_.find(
id);
419 if (it != peers_.end())
421 JLOG(journal_.trace())
422 <<
"deletePeer: " <<
Slice(
validator) <<
" " <<
id <<
" selected "
424 << (considered_.find(
id) != considered_.end()) <<
" erase "
426 auto now = clock_type::now();
429 for (
auto& [k, v] : peers_)
439 reachedThreshold_ = 0;
442 else if (considered_.find(
id) != considered_.end())
446 considered_.erase(
id);
449 it->second.lastMessage = now;
450 it->second.count = 0;
457template <
typename clock_type>
461 for (
auto& [_, peer] : peers_)
468template <
typename clock_type>
474 reachedThreshold_ = 0;
478template <
typename clock_type>
482 return std::count_if(peers_.begin(), peers_.end(), [&](
auto const& it) {
483 return (it.second.state == state);
487template <
typename clock_type>
491 return std::count_if(peers_.begin(), peers_.end(), [&](
auto const& it) {
492 return (it.second.state != state);
496template <
typename clock_type>
501 for (
auto const& [
id, info] : peers_)
507template <
typename clock_type>
518 for (
auto const& [
id, info] : peers_)
524 epoch<milliseconds>(info.expire).count(),
525 epoch<milliseconds>(info.lastMessage).count()))));
534template <
typename clock_type>
566 protocol::MessageType type);
580 return it->second.inState(state);
590 return it->second.notInState(state);
600 return it->second.state_ == state;
610 return it->second.getSelected();
624 return it->second.getPeers();
634 return it->second.getState();
663 beast::get_abstract_clock<clock_type>()};
666template <
typename clock_type>
674 auto it = peersWithMessage_.find(key);
675 if (it == peersWithMessage_.end())
677 JLOG(journal_.trace())
678 <<
"addPeerMessage: new " <<
to_string(key) <<
" " << id;
683 if (it->second.find(
id) != it->second.end())
685 JLOG(journal_.trace()) <<
"addPeerMessage: duplicate message "
690 JLOG(journal_.trace())
691 <<
"addPeerMessage: added " <<
to_string(key) <<
" " << id;
699template <
typename clock_type>
705 protocol::MessageType type)
707 if (!addPeerMessage(key,
id))
711 if (it == slots_.end())
713 JLOG(journal_.trace())
726template <
typename clock_type>
734template <
typename clock_type>
738 auto now = clock_type::now();
740 for (
auto it = slots_.begin(); it != slots_.end();)
742 it->second.deleteIdlePeer(it->first);
745 JLOG(journal_.trace())
746 <<
"deleteIdlePeers: deleting idle slot " <<
Slice(it->first);
747 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.
time_point const & getLastSelected() const
Get the time of the last peer selection round.
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.
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
std::unique_ptr< Config > validator(std::unique_ptr< Config >, std::string const &)
adjust configuration with params needed to be a validator
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.