3#include <xrpld/core/Config.h>
4#include <xrpld/overlay/Peer.h>
5#include <xrpld/overlay/ReduceRelayCommon.h>
7#include <xrpl/basics/Log.h>
8#include <xrpl/basics/chrono.h>
9#include <xrpl/basics/random.h>
10#include <xrpl/beast/container/aged_unordered_map.h>
11#include <xrpl/beast/utility/Journal.h>
12#include <xrpl/protocol/PublicKey.h>
13#include <xrpl/protocol/messages.h>
24namespace reduce_relay {
26template <
typename clock_type>
41template <
typename Unit,
typename TP>
45 return std::chrono::duration_cast<Unit>(t.time_since_epoch());
83template <
typename clock_type>
87 friend class Slots<clock_type>;
233template <
typename clock_type>
238 auto now = clock_type::now();
239 for (
auto it = peers_.begin(); it != peers_.end();)
241 auto& peer = it->second;
244 if (now - peer.lastMessage >
IDLED)
246 JLOG(journal_.trace()) <<
"deleteIdlePeer: " <<
Slice(validator) <<
" " <<
id <<
" idled "
247 << duration_cast<seconds>(now - peer.lastMessage).count() <<
" selected "
249 deletePeer(validator,
id,
false);
254template <
typename clock_type>
259 protocol::MessageType type,
263 auto now = clock_type::now();
264 auto it = peers_.find(
id);
266 if (it == peers_.end())
268 JLOG(journal_.trace()) <<
"update: adding peer " <<
Slice(validator) <<
" " << id;
276 JLOG(journal_.trace()) <<
"update: squelch expired " <<
Slice(validator) <<
" " << id;
278 it->second.lastMessage = now;
283 auto& peer = it->second;
285 JLOG(journal_.trace()) <<
"update: existing peer " <<
Slice(validator) <<
" " <<
id <<
" slot state "
286 <<
static_cast<int>(state_) <<
" peer state " <<
static_cast<int>(peer.state) <<
" count "
287 << peer.count <<
" last " << duration_cast<milliseconds>(now - peer.lastMessage).count()
288 <<
" pool " << considered_.
size() <<
" threshold " << reachedThreshold_ <<
" "
289 << (type == protocol::mtVALIDATION ?
"validation" :
"proposal");
291 peer.lastMessage = now;
301 considered_.insert(
id);
307 JLOG(journal_.trace()) <<
"update: resetting due to inactivity " <<
Slice(validator) <<
" " <<
id <<
" "
308 << duration_cast<seconds>(now - lastSelected_).count();
313 if (reachedThreshold_ == maxSelectedPeers_)
322 auto const consideredPoolSize = considered_.
size();
323 while (selected.
size() != maxSelectedPeers_ && considered_.size() != 0)
325 auto i = considered_.size() == 1 ? 0 :
rand_int(considered_.size() - 1);
326 auto it =
std::next(considered_.begin(), i);
328 considered_.erase(it);
329 auto const& itPeers = peers_.find(
id);
330 if (itPeers == peers_.end())
332 JLOG(journal_.error()) <<
"update: peer not found " <<
Slice(validator) <<
" " << id;
335 if (now - itPeers->second.lastMessage <
IDLED)
339 if (selected.
size() != maxSelectedPeers_)
341 JLOG(journal_.trace()) <<
"update: selection failed " <<
Slice(validator) <<
" " << id;
348 auto s = selected.
begin();
349 JLOG(journal_.trace()) <<
"update: " <<
Slice(validator) <<
" " <<
id <<
" pool size " << consideredPoolSize
352 XRPL_ASSERT(peers_.size() >= maxSelectedPeers_,
"xrpl::reduce_relay::Slot::update : minimum peers");
357 for (
auto& [k, v] : peers_)
361 if (selected.
find(k) != selected.
end())
365 if (journal_.trace())
373 JLOG(journal_.trace()) <<
"update: squelching " <<
Slice(validator) <<
" " <<
id <<
" " << str.
str();
375 reachedThreshold_ = 0;
380template <
typename clock_type>
389 JLOG(journal_.warn()) <<
"getSquelchDuration: unexpected squelch duration " << npeers;
394template <
typename clock_type>
398 auto it = peers_.find(
id);
399 if (it != peers_.end())
403 JLOG(journal_.trace()) <<
"deletePeer: " <<
Slice(validator) <<
" " <<
id <<
" selected "
405 << (considered_.find(
id) != considered_.end()) <<
" erase " <<
erase;
406 auto now = clock_type::now();
409 for (
auto& [k, v] : peers_)
419 reachedThreshold_ = 0;
422 else if (considered_.find(
id) != considered_.end())
426 considered_.erase(
id);
429 it->second.lastMessage = now;
430 it->second.count = 0;
436 for (
auto const& k : toUnsquelch)
437 handler_.unsquelch(validator, k);
441template <
typename clock_type>
445 for (
auto& [_, peer] : peers_)
452template <
typename clock_type>
458 reachedThreshold_ = 0;
462template <
typename clock_type>
466 return std::count_if(peers_.begin(), peers_.end(), [&](
auto const& it) { return (it.second.state == state); });
469template <
typename clock_type>
473 return std::count_if(peers_.begin(), peers_.end(), [&](
auto const& it) { return (it.second.state != state); });
476template <
typename clock_type>
481 for (
auto const& [
id, info] : peers_)
487template <
typename clock_type>
494 for (
auto const& [
id, info] : peers_)
500 epoch<milliseconds>(info.expire).count(),
501 epoch<milliseconds>(info.lastMessage).count()))));
510template <
typename clock_type>
577 protocol::MessageType type,
590 auto const& it =
slots_.find(validator);
592 return it->second.inState(state);
600 auto const& it =
slots_.find(validator);
602 return it->second.notInState(state);
610 auto const& it =
slots_.find(validator);
612 return it->second.state_ == state;
620 auto const& it =
slots_.find(validator);
622 return it->second.getSelected();
632 auto const& it =
slots_.find(validator);
634 return it->second.getPeers();
642 auto const& it =
slots_.find(validator);
644 return it->second.getState();
681template <
typename clock_type>
689 auto it = peersWithMessage_.find(key);
690 if (it == peersWithMessage_.end())
692 JLOG(journal_.trace()) <<
"addPeerMessage: new " <<
to_string(key) <<
" " << id;
697 if (it->second.find(
id) != it->second.end())
699 JLOG(journal_.trace()) <<
"addPeerMessage: duplicate message " <<
to_string(key) <<
" " << id;
703 JLOG(journal_.trace()) <<
"addPeerMessage: added " <<
to_string(key) <<
" " << id;
711template <
typename clock_type>
717 protocol::MessageType type,
720 if (!addPeerMessage(key,
id))
723 auto it = slots_.find(validator);
724 if (it == slots_.end())
726 JLOG(journal_.trace()) <<
"updateSlotAndSquelch: new slot " <<
Slice(validator);
729 validator,
Slot<clock_type>(handler_, logs_.journal(
"Slot"), maxSelectedPeers_)))
731 it->second.update(validator,
id, type, callback);
734 it->second.update(validator,
id, type, callback);
737template <
typename clock_type>
741 for (
auto& [validator, slot] : slots_)
742 slot.deletePeer(validator,
id,
erase);
745template <
typename clock_type>
749 auto now = clock_type::now();
751 for (
auto it = slots_.begin(); it != slots_.end();)
753 it->second.deleteIdlePeer(it->first);
756 JLOG(journal_.trace()) <<
"deleteIdlePeers: deleting idle slot " <<
Slice(it->first);
757 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.
beast::Journal const journal_
std::uint16_t reachedThreshold_
std::set< id_t > getSelected() const
Return selected peers.
std::uint16_t inState(PeerState state) const
Return number of peers in state.
uint16_t const maxSelectedPeers_
void update(PublicKey const &validator, id_t id, protocol::MessageType type, ignored_squelch_callback callback)
Update peer info.
typename clock_type::time_point time_point
void initCounting()
Initialize slot to Counting state.
std::uint16_t notInState(PeerState state) const
Return number of peers not in state.
std::unordered_map< id_t, PeerInfo > peers_
void deleteIdlePeer(PublicKey const &validator)
Check if peers stopped relaying messages.
time_point const & getLastSelected() const
Get the time of the last peer selection round.
std::unordered_map< id_t, std::tuple< PeerState, uint16_t, uint32_t, uint32_t > > getPeers() const
Get peers info.
SquelchHandler const & handler_
void resetCounts()
Reset counts of peers in Selected or Counting state.
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::unordered_set< id_t > considered_
clock_type::time_point lastSelected_
std::chrono::seconds getSquelchDuration(std::size_t npeers)
Get random squelch duration between MIN_UNSQUELCH_EXPIRE and min(max(MAX_UNSQUELCH_EXPIRE_DEFAULT,...
SlotState getState() const
Return Slot's state.
Slots is a container for validator's Slot and handles Slot update when a message is received from a v...
hash_map< PublicKey, Slot< clock_type > > slots_
bool addPeerMessage(uint256 const &key, id_t id)
Add message/peer if have not seen this message from the peer.
std::optional< SlotState > getState(PublicKey const &validator)
Get Slot's state.
bool baseSquelchReady()
Check if base squelching feature is enabled and ready.
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 const baseSquelchEnabled_
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.
bool reduceRelayReady()
Check if reduce_relay::WAIT_ON_BOOTUP time passed since startup.
beast::Journal const journal_
Slots(Logs &logs, SquelchHandler const &handler, Config const &config)
SquelchHandler const & handler_
std::atomic_bool reduceRelayReady_
std::optional< std::uint16_t > inState(PublicKey const &validator, PeerState state) const
Return number of peers in state.
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.
bool inState(PublicKey const &validator, SlotState state) const
Return true if Slot is in state.
void deletePeer(id_t id, bool erase)
Called when a peer is deleted.
std::unordered_map< typename Peer::id_t, std::tuple< PeerState, uint16_t, uint32_t, std::uint32_t > > getPeers(PublicKey const &validator)
Get peers info.
static messages peersWithMessage_
void deleteIdlePeers()
Check if peers stopped relaying messages and if slots stopped receiving messages from the validator.
uint16_t const maxSelectedPeers_
typename clock_type::time_point time_point
virtual ~SquelchHandler()
virtual void unsquelch(PublicKey const &validator, Peer::id_t id) const =0
Unsquelch handler.
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 IDLED
static constexpr auto MAX_UNSQUELCH_EXPIRE_DEFAULT
static constexpr uint16_t MIN_MESSAGE_THRESHOLD
static constexpr auto WAIT_ON_BOOTUP
static constexpr auto SQUELCH_PER_PEER
static constexpr auto MAX_UNSQUELCH_EXPIRE_PEERS
static constexpr auto MIN_UNSQUELCH_EXPIRE
static constexpr uint16_t MAX_MESSAGE_THRESHOLD
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string to_string(base_uint< Bits, Tag > const &a)
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.
Data maintained for each peer.