improves code readability

This commit is contained in:
Vito
2025-06-27 11:56:14 +02:00
parent 5e6189d656
commit dcb832ecf9
5 changed files with 34 additions and 31 deletions

View File

@@ -124,7 +124,7 @@ public:
void
squelchValidator(PublicKey const& validatorKey, Peer::id_t peerID)
{
Slots::squelchValidator(validatorKey, peerID);
Slots::registerSquelchedValidator(validatorKey, peerID);
}
bool
@@ -261,7 +261,7 @@ vp_enhanced_squelch_enable=0
auto const validator = randomKeyPair(KeyType::ed25519).first;
uint256 message{0};
slots.updateValidatorSlot(message, validator, peerID);
slots.updateUntrustedValidatorSlot(message, validator, peerID);
// adding untrusted slot does not effect trusted slots
BEAST_EXPECTS(
@@ -305,10 +305,11 @@ vp_enhanced_squelch_enable=0
slots.squelchValidator(validator, squelchedPeerID);
// this should not trigger squelch assertions, the peer is squelched
slots.updateValidatorSlot(
slots.updateUntrustedValidatorSlot(
sha512Half(validator), validator, squelchedPeerID);
slots.updateValidatorSlot(sha512Half(validator), validator, newPeerID);
slots.updateUntrustedValidatorSlot(
sha512Half(validator), validator, newPeerID);
// the squelched peer remained squelched
BEAST_EXPECTS(
@@ -350,7 +351,7 @@ vp_enhanced_squelch_enable=0
// simulate additional messages from already selected validators
for (auto const& validator : validators)
for (int i = 0; i < reduce_relay::MAX_MESSAGE_THRESHOLD; ++i)
slots.updateValidatorSlot(
slots.updateUntrustedValidatorSlot(
sha512Half(validator) + static_cast<uint256>(i),
validator,
peerID);
@@ -374,7 +375,7 @@ vp_enhanced_squelch_enable=0
slots.squelchValidator(key, peerID);
};
slots.updateValidatorSlot(
slots.updateUntrustedValidatorSlot(
sha512Half(newValidator), newValidator, peerID);
// Once the slots are saturated every other validator is squelched
@@ -779,7 +780,7 @@ private:
++j)
// send enough messages so that a validator slot is selected
for (int k = 0; k < reduce_relay::MAX_MESSAGE_THRESHOLD; ++k)
slots.updateValidatorSlot(
slots.updateUntrustedValidatorSlot(
sha512Half(validator) + static_cast<uint256>(k),
validator,
j);

View File

@@ -325,7 +325,7 @@ public:
config.VP_REDUCE_RELAY_ENHANCED_SQUELCH_ENABLE)
, clock_(clock)
, peersWithMessage_(clock)
, peersWithValidators_(clock)
, peersWithSquelchedValidators_(clock)
{
}
@@ -356,12 +356,12 @@ public:
* @param id The ID of the peer that sent the message
*/
void
updateValidatorSlot(
updateUntrustedValidatorSlot(
uint256 const& key,
PublicKey const& validator,
Peer::id_t id)
{
updateValidatorSlot(key, validator, id, []() {});
updateUntrustedValidatorSlot(key, validator, id, []() {});
}
/** Updates untrusted validator slot. Do not call for trusted
@@ -372,7 +372,7 @@ public:
* @param callback A callback to report ignored validations
*/
void
updateValidatorSlot(
updateUntrustedValidatorSlot(
uint256 const& key,
PublicKey const& validator,
Peer::id_t id,
@@ -433,7 +433,9 @@ public:
* @param peerID peer ID
*/
void
squelchValidator(PublicKey const& validatorKey, Peer::id_t peerID);
registerSquelchedValidator(
PublicKey const& validatorKey,
Peer::id_t peerID);
void
onWrite(beast::PropertyStream::Map& stream) const;
@@ -502,7 +504,7 @@ protected:
// Maintain aged container of validator/peers. This is used to track
// which validator/peer were squelced. A peer that whose squelch
// has expired is removed.
validators peersWithValidators_;
validators peersWithSquelchedValidators_;
struct ValidatorInfo
{
@@ -512,6 +514,7 @@ protected:
// message for this validator
};
// Untrusted validators considered for open untrusted slots
hash_map<PublicKey, ValidatorInfo> consideredValidators_;
};

View File

@@ -1422,7 +1422,7 @@ OverlayImpl::squelchAll(PublicKey const& validator, uint32_t squelchDuration)
{
for_each([&](std::shared_ptr<PeerImp>&& p) {
p->send(makeSquelchMessage(validator, true, squelchDuration));
slots_.squelchValidator(validator, p->id());
slots_.registerSquelchedValidator(validator, p->id());
});
}
@@ -1494,7 +1494,7 @@ OverlayImpl::updateValidatorSlot(
updateValidatorSlot(key, validator, peer);
});
slots_.updateValidatorSlot(key, validator, peer, [&]() {
slots_.updateUntrustedValidatorSlot(key, validator, peer, [&]() {
reportInboundTraffic(TrafficCount::squelch_ignored, 0);
});
}

View File

@@ -2704,6 +2704,7 @@ PeerImp::onMessage(std::shared_ptr<protocol::TMSquelch> const& m)
fee_.update(Resource::feeInvalidData, "squelch no pubkey");
return;
}
auto validator = m->validatorpubkey();
auto const slice{makeSlice(validator)};
if (!publicKeyType(slice))

View File

@@ -323,25 +323,22 @@ Slots::reduceRelayReady()
}
void
Slots::squelchValidator(PublicKey const& validatorKey, Peer::id_t peerID)
Slots::registerSquelchedValidator(
PublicKey const& validatorKey,
Peer::id_t peerID)
{
auto it = peersWithValidators_.find(validatorKey);
if (it == peersWithValidators_.end())
peersWithValidators_.emplace(
validatorKey, std::unordered_set<Peer::id_t>{peerID});
else if (it->second.find(peerID) == it->second.end())
it->second.insert(peerID);
peersWithSquelchedValidators_[validatorKey].insert(peerID);
}
bool
Slots::expireAndIsValidatorSquelched(PublicKey const& validatorKey)
{
beast::expire(
peersWithValidators_, reduce_relay::MAX_UNSQUELCH_EXPIRE_DEFAULT);
peersWithSquelchedValidators_,
reduce_relay::MAX_UNSQUELCH_EXPIRE_DEFAULT);
return peersWithValidators_.find(validatorKey) !=
peersWithValidators_.end();
return peersWithSquelchedValidators_.find(validatorKey) !=
peersWithSquelchedValidators_.end();
}
bool
@@ -350,12 +347,13 @@ Slots::expireAndIsPeerSquelched(
Peer::id_t peerID)
{
beast::expire(
peersWithValidators_, reduce_relay::MAX_UNSQUELCH_EXPIRE_DEFAULT);
peersWithSquelchedValidators_,
reduce_relay::MAX_UNSQUELCH_EXPIRE_DEFAULT);
auto const it = peersWithValidators_.find(validatorKey);
auto const it = peersWithSquelchedValidators_.find(validatorKey);
// if validator was not squelched, the peer was also not squelched
if (it == peersWithValidators_.end())
if (it == peersWithSquelchedValidators_.end())
return false;
// if a peer is found the squelch for it has not expired
@@ -438,7 +436,7 @@ Slots::updateSlotAndSquelch(
}
void
Slots::updateValidatorSlot(
Slots::updateUntrustedValidatorSlot(
uint256 const& key,
PublicKey const& validator,
Peer::id_t id,
@@ -462,7 +460,7 @@ Slots::updateValidatorSlot(
{
if (!expireAndIsPeerSquelched(validator, id))
{
squelchValidator(validator, id);
registerSquelchedValidator(validator, id);
handler_.squelch(
validator, id, MAX_UNSQUELCH_EXPIRE_DEFAULT.count());
}