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>;
131 protocol::MessageType type,
235template <
typename clock_type>
240 auto now = clock_type::now();
241 for (
auto it = peers_.begin(); it != peers_.end();)
243 auto& peer = it->second;
246 if (now - peer.lastMessage >
IDLED)
248 JLOG(journal_.trace())
249 <<
"deleteIdlePeer: " <<
Slice(validator) <<
" " <<
id <<
" idled "
250 << duration_cast<seconds>(now - peer.lastMessage).count() <<
" selected "
252 deletePeer(validator,
id,
false);
257template <
typename clock_type>
262 protocol::MessageType type,
266 auto now = clock_type::now();
267 auto it = peers_.find(
id);
269 if (it == peers_.end())
271 JLOG(journal_.trace()) <<
"update: adding peer " <<
Slice(validator) <<
" " << id;
279 JLOG(journal_.trace()) <<
"update: squelch expired " <<
Slice(validator) <<
" " << id;
281 it->second.lastMessage = now;
286 auto& peer = it->second;
288 JLOG(journal_.trace()) <<
"update: existing peer " <<
Slice(validator) <<
" " <<
id
289 <<
" slot state " <<
static_cast<int>(state_) <<
" peer state "
290 <<
static_cast<int>(peer.state) <<
" count " << peer.count <<
" last "
291 << duration_cast<milliseconds>(now - peer.lastMessage).count()
292 <<
" pool " << considered_.
size() <<
" threshold " << reachedThreshold_
293 <<
" " << (type == protocol::mtVALIDATION ?
"validation" :
"proposal");
295 peer.lastMessage = now;
305 considered_.insert(
id);
311 JLOG(journal_.trace()) <<
"update: resetting due to inactivity " <<
Slice(validator) <<
" "
312 <<
id <<
" " << duration_cast<seconds>(now - lastSelected_).count();
317 if (reachedThreshold_ == maxSelectedPeers_)
326 auto const consideredPoolSize = considered_.
size();
327 while (selected.
size() != maxSelectedPeers_ && considered_.size() != 0)
329 auto i = considered_.size() == 1 ? 0 :
rand_int(considered_.size() - 1);
330 auto it =
std::next(considered_.begin(), i);
332 considered_.erase(it);
333 auto const& itPeers = peers_.find(
id);
334 if (itPeers == peers_.end())
336 JLOG(journal_.error())
337 <<
"update: peer not found " <<
Slice(validator) <<
" " << id;
340 if (now - itPeers->second.lastMessage <
IDLED)
344 if (selected.
size() != maxSelectedPeers_)
346 JLOG(journal_.trace()) <<
"update: selection failed " <<
Slice(validator) <<
" " << id;
353 auto s = selected.
begin();
354 JLOG(journal_.trace()) <<
"update: " <<
Slice(validator) <<
" " <<
id <<
" pool size "
355 << consideredPoolSize <<
" selected " << *s <<
" "
359 peers_.size() >= maxSelectedPeers_,
"xrpl::reduce_relay::Slot::update : minimum peers");
364 for (
auto& [k, v] : peers_)
368 if (selected.
find(k) != selected.
end())
372 if (journal_.trace())
376 getSquelchDuration(peers_.size() - maxSelectedPeers_);
381 JLOG(journal_.trace()) <<
"update: squelching " <<
Slice(validator) <<
" " <<
id <<
" "
384 reachedThreshold_ = 0;
389template <
typename clock_type>
398 JLOG(journal_.warn()) <<
"getSquelchDuration: unexpected squelch duration " << npeers;
403template <
typename clock_type>
407 auto it = peers_.find(
id);
408 if (it != peers_.end())
412 JLOG(journal_.trace()) <<
"deletePeer: " <<
Slice(validator) <<
" " <<
id <<
" selected "
414 << (considered_.find(
id) != considered_.end()) <<
" erase " <<
erase;
415 auto now = clock_type::now();
418 for (
auto& [k, v] : peers_)
428 reachedThreshold_ = 0;
431 else if (considered_.find(
id) != considered_.end())
435 considered_.erase(
id);
438 it->second.lastMessage = now;
439 it->second.count = 0;
445 for (
auto const& k : toUnsquelch)
446 handler_.unsquelch(validator, k);
450template <
typename clock_type>
454 for (
auto& [_, peer] : peers_)
461template <
typename clock_type>
467 reachedThreshold_ = 0;
471template <
typename clock_type>
476 peers_.begin(), peers_.end(), [&](
auto const& it) { return (it.second.state == state); });
479template <
typename clock_type>
484 peers_.begin(), peers_.end(), [&](
auto const& it) { return (it.second.state != state); });
487template <
typename clock_type>
492 for (
auto const& [
id, info] : peers_)
498template <
typename clock_type>
506 for (
auto const& [
id, info] : peers_)
514 epoch<milliseconds>(info.expire).count(),
515 epoch<milliseconds>(info.lastMessage).count()))));
524template <
typename clock_type>
543 ,
logs_(registry.getLogs())
544 ,
journal_(registry.getJournal(
"Slots"))
563 reduceRelayReady_ = reduce_relay::epoch<std::chrono::minutes>(clock_type::now()) >
581 protocol::MessageType type)
598 protocol::MessageType type,
611 auto const& it =
slots_.find(validator);
613 return it->second.inState(state);
621 auto const& it =
slots_.find(validator);
623 return it->second.notInState(state);
631 auto const& it =
slots_.find(validator);
633 return it->second.state_ == state;
641 auto const& it =
slots_.find(validator);
643 return it->second.getSelected();
654 auto const& it =
slots_.find(validator);
656 return it->second.getPeers();
664 auto const& it =
slots_.find(validator);
666 return it->second.getState();
703template <
typename clock_type>
711 auto it = peersWithMessage_.find(key);
712 if (it == peersWithMessage_.end())
714 JLOG(journal_.trace()) <<
"addPeerMessage: new " <<
to_string(key) <<
" " << id;
719 if (it->second.find(
id) != it->second.end())
721 JLOG(journal_.trace())
722 <<
"addPeerMessage: duplicate message " <<
to_string(key) <<
" " << id;
726 JLOG(journal_.trace()) <<
"addPeerMessage: added " <<
to_string(key) <<
" " << id;
734template <
typename clock_type>
740 protocol::MessageType type,
743 if (!addPeerMessage(key,
id))
746 auto it = slots_.find(validator);
747 if (it == slots_.end())
749 JLOG(journal_.trace()) <<
"updateSlotAndSquelch: new slot " <<
Slice(validator);
756 it->second.update(validator,
id, type, callback);
759 it->second.update(validator,
id, type, callback);
762template <
typename clock_type>
766 for (
auto& [validator, slot] : slots_)
767 slot.deletePeer(validator,
id,
erase);
770template <
typename clock_type>
774 auto now = clock_type::now();
776 for (
auto it = slots_.begin(); it != slots_.end();)
778 it->second.deleteIdlePeer(it->first);
781 JLOG(journal_.trace()) <<
"deleteIdlePeers: deleting idle slot " <<
Slice(it->first);
782 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.
Service registry for dependency injection.
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::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< 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_
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.
Slots(ServiceRegistry ®istry, SquelchHandler const &handler, Config const &config)
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.
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.