20#include <xrpld/app/consensus/RCLValidations.h>
21#include <xrpld/app/ledger/Ledger.h>
22#include <xrpld/app/misc/NegativeUNLVote.h>
23#include <xrpld/shamap/SHAMapItem.h>
49 for (
auto const& k : unlKeys)
61 auto negUnlKeys = prevLedger->negativeUNL();
62 auto negUnlToDisable = prevLedger->validatorToDisable();
63 auto negUnlToReEnable = prevLedger->validatorToReEnable();
65 negUnlKeys.insert(*negUnlToDisable);
67 negUnlKeys.erase(*negUnlToReEnable);
70 for (
auto const& k : negUnlKeys)
74 if (!nidToKeyMap.
count(nid))
80 auto const seq = prevLedger->info().seq + 1;
84 auto const candidates =
88 if (!candidates.toDisableCandidates.empty())
91 choose(prevLedger->info().hash, candidates.toDisableCandidates);
94 "ripple::NegativeUNLVote::doVoting : found node to disable");
98 if (!candidates.toReEnableCandidates.empty())
101 prevLedger->info().hash, candidates.toReEnableCandidates);
104 "ripple::NegativeUNLVote::doVoting : found node to enable");
117 STTx negUnlTx(ttUNL_MODIFY, [&](
auto& obj) {
118 obj.setFieldU8(sfUNLModifyDisabling, modify ==
ToDisable ? 1 : 0);
119 obj.setFieldU32(sfLedgerSequence, seq);
120 obj.setFieldVL(sfUNLModifyValidator, vp.
slice());
125 if (!initialSet->addGiveItem(
129 JLOG(
j_.
warn()) <<
"N-UNL: ledger seq=" << seq
130 <<
", add ttUNL_MODIFY tx failed";
134 JLOG(
j_.
debug()) <<
"N-UNL: ledger seq=" << seq
135 <<
", add a ttUNL_MODIFY Tx with txID: "
136 << negUnlTx.getTransactionID() <<
", the validator to "
137 << (modify ==
ToDisable ?
"disable: " :
"re-enable: ")
149 "ripple::NegativeUNLVote::choose : non-empty input");
152 NodeID txNodeID = candidates[0];
153 for (
int j = 1; j < candidates.
size(); ++j)
155 if ((candidates[j] ^ randomPad) < (txNodeID ^ randomPad))
157 txNodeID = candidates[j];
175 auto const seq = prevLedger->info().seq + 1;
179 auto const hashIndex = prevLedger->read(
keylet::skip());
180 if (!hashIndex || !hashIndex->isFieldPresent(sfHashes))
182 JLOG(
j_.
debug()) <<
"N-UNL: ledger " << seq <<
" no history.";
185 auto const ledgerAncestors = hashIndex->getFieldV256(sfHashes).value();
186 auto const numAncestors = ledgerAncestors.size();
189 JLOG(
j_.
debug()) <<
"N-UNL: ledger " << seq
190 <<
" not enough history. Can trace back only "
191 << numAncestors <<
" ledgers.";
197 for (
auto const& k : unl)
207 ledgerAncestors[numAncestors - 1 - i], seq - 2 - i))
209 if (scoreTable.
count(v->getNodeID()))
210 ++scoreTable[v->getNodeID()];
217 if (
auto const it = scoreTable.
find(
myId_); it != scoreTable.
end())
223 JLOG(
j_.
debug()) <<
"N-UNL: ledger " << seq
224 <<
". Local node only issued " << myValidationCount
227 <<
" The reliability measurement could be wrong.";
240 JLOG(
j_.
error()) <<
"N-UNL: ledger " << seq <<
". Local node issued "
241 << myValidationCount <<
" validations in last "
254 auto const canAdd = [&]() ->
bool {
255 auto const maxNegativeListed =
static_cast<std::size_t>(
258 for (
auto const& n : unl)
263 bool const result = negativeListed < maxNegativeListed;
264 JLOG(
j_.
trace()) <<
"N-UNL: nodeId " <<
myId_ <<
" lowWaterMark "
267 <<
" negativeListed " << negativeListed
268 <<
" maxNegativeListed " << maxNegativeListed;
273 for (
auto const& [nodeId, score] : scoreTable)
275 JLOG(
j_.
trace()) <<
"N-UNL: node " << nodeId <<
" score " << score;
285 JLOG(
j_.
trace()) <<
"N-UNL: toDisable candidate " << nodeId;
294 JLOG(
j_.
trace()) <<
"N-UNL: toReEnable candidate " << nodeId;
311 for (
auto const& n : negUnl)
328 for (
auto const& n : nowTrusted)
332 JLOG(
j_.
trace()) <<
"N-UNL: add a new validator " << n
333 <<
" at ledger seq=" << seq;
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
static constexpr size_t negativeUNLHighWaterMark
An unreliable validator must have more than negativeUNLHighWaterMark validations in the last flag led...
NodeID choose(uint256 const &randomPadData, std::vector< NodeID > const &candidates)
Pick one candidate from a vector of candidates.
std::optional< hash_map< NodeID, std::uint32_t > > buildScoreTable(std::shared_ptr< Ledger const > const &prevLedger, hash_set< NodeID > const &unl, RCLValidations &validations)
Build a reliability measurement score table of validators' validation messages in the last flag ledge...
NegativeUNLModify
A flag indicating whether a UNLModify Tx is to disable or to re-enable a validator.
void purgeNewValidators(LedgerIndex seq)
Purge validators that are not new anymore.
Candidates const findAllCandidates(hash_set< NodeID > const &unl, hash_set< NodeID > const &negUnl, hash_map< NodeID, std::uint32_t > const &scoreTable)
Process the score table and find all disabling and re-enabling candidates.
static constexpr size_t newValidatorDisableSkip
We don't want to disable new validators immediately after adding them.
static constexpr size_t negativeUNLLowWaterMark
A validator is considered unreliable if its validations is less than negativeUNLLowWaterMark in the l...
void doVoting(std::shared_ptr< Ledger const > const &prevLedger, hash_set< PublicKey > const &unlKeys, RCLValidations &validations, std::shared_ptr< SHAMap > const &initialSet)
Cast our local vote on the NegativeUNL candidates.
hash_map< NodeID, LedgerIndex > newValidators_
void newValidators(LedgerIndex seq, hash_set< NodeID > const &nowTrusted)
Notify NegativeUNLVote that new validators are added.
static constexpr float negativeUNLMaxListed
We only want to put 25% of the UNL on the NegativeUNL.
void addTx(LedgerIndex seq, PublicKey const &vp, NegativeUNLModify modify, std::shared_ptr< SHAMap > const &initialSet)
Add a ttUNL_MODIFY Tx to the transaction set.
NegativeUNLVote(NodeID const &myId, beast::Journal j)
Constructor.
static constexpr size_t negativeUNLMinLocalValsToVote
The minimum number of validations of the local node for it to participate in the voting.
Slice slice() const noexcept
std::vector< WrappedValidationType > getTrustedForLedger(ID const &ledgerID, Seq const &seq)
Get trusted full validations for a specific ledger.
void setSeqToKeep(Seq const &low, Seq const &high)
Set the range [low, high) of validations to keep from expire.
static std::size_t constexpr bytes
static base_uint fromVoid(void const *data)
Keylet const & skip() noexcept
The index of the "short" skip list.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool canAdd(STAmount const &amt1, STAmount const &amt2)
Safely checks if two STAmount values can be added without overflow, underflow, or precision loss.
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
boost::intrusive_ptr< SHAMapItem > make_shamapitem(uint256 const &tag, Slice data)
std::uint32_t constexpr FLAG_LEDGER_INTERVAL
std::vector< NodeID > toReEnableCandidates
std::vector< NodeID > toDisableCandidates