20 #include <ripple/app/consensus/RCLValidations.h>
21 #include <ripple/app/ledger/Ledger.h>
22 #include <ripple/app/misc/NegativeUNLVote.h>
23 #include <ripple/app/misc/ValidatorList.h>
24 #include <ripple/app/tx/apply.h>
25 #include <ripple/basics/Log.h>
26 #include <ripple/beast/unit_test.h>
27 #include <ripple/ledger/View.h>
86 hash_map<PublicKey, std::uint32_t> nUnlLedgerSeq);
227 testcase(
"Create UNLModify Tx and apply to ledgers");
232 auto l = std::make_shared<Ledger>(
245 l = std::make_shared<Ledger>(
248 auto txDisable_0 =
createTx(
true, l->seq(), publicKeys[0]);
249 auto txReEnable_1 =
createTx(
false, l->seq(), publicKeys[1]);
261 for (
auto i = 0; i < 256 - 2; ++i)
263 l = std::make_shared<Ledger>(
266 BEAST_EXPECT(l->isFlagLedger());
267 l->updateNegativeUNL();
269 auto txDisable_0 =
createTx(
true, l->seq(), publicKeys[0]);
270 auto txDisable_1 =
createTx(
true, l->seq(), publicKeys[1]);
271 auto txReEnable_2 =
createTx(
false, l->seq(), publicKeys[2]);
281 BEAST_EXPECT(good_size);
284 BEAST_EXPECT(l->validatorToDisable() == publicKeys[0]);
286 uint256 txID = txDisable_0.getTransactionID();
287 BEAST_EXPECT(l->txExists(txID));
293 for (
auto i = 0; i < 256; ++i)
296 BEAST_EXPECT(good_size);
298 BEAST_EXPECT(l->validatorToDisable() == publicKeys[0]);
299 l = std::make_shared<Ledger>(
302 BEAST_EXPECT(l->isFlagLedger());
303 l->updateNegativeUNL();
308 BEAST_EXPECT(good_size);
311 BEAST_EXPECT(*(l->negativeUNL().begin()) == publicKeys[0]);
312 nUnlLedgerSeq.
emplace(publicKeys[0], l->seq());
315 auto txDisable_0 =
createTx(
true, l->seq(), publicKeys[0]);
316 auto txDisable_1 =
createTx(
true, l->seq(), publicKeys[1]);
317 auto txReEnable_0 =
createTx(
false, l->seq(), publicKeys[0]);
318 auto txReEnable_1 =
createTx(
false, l->seq(), publicKeys[1]);
319 auto txReEnable_2 =
createTx(
false, l->seq(), publicKeys[2]);
329 BEAST_EXPECT(good_size);
332 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
333 BEAST_EXPECT(l->validatorToDisable() == publicKeys[1]);
334 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[0]);
342 for (
auto i = 0; i < 256; ++i)
345 BEAST_EXPECT(good_size);
348 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
349 BEAST_EXPECT(l->validatorToDisable() == publicKeys[1]);
350 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[0]);
352 l = std::make_shared<Ledger>(
355 BEAST_EXPECT(l->isFlagLedger());
356 l->updateNegativeUNL();
361 BEAST_EXPECT(good_size);
364 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
367 auto txDisable_0 =
createTx(
true, l->seq(), publicKeys[0]);
373 BEAST_EXPECT(good_size);
376 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
377 BEAST_EXPECT(l->validatorToDisable() == publicKeys[0]);
378 nUnlLedgerSeq.
emplace(publicKeys[1], l->seq());
379 nUnlLedgerSeq.
erase(publicKeys[0]);
386 for (
auto i = 0; i < 256; ++i)
389 BEAST_EXPECT(good_size);
392 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
393 BEAST_EXPECT(l->validatorToDisable() == publicKeys[0]);
395 l = std::make_shared<Ledger>(
398 BEAST_EXPECT(l->isFlagLedger());
399 l->updateNegativeUNL();
404 BEAST_EXPECT(good_size);
407 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
408 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
409 nUnlLedgerSeq.
emplace(publicKeys[0], l->seq());
413 auto txDisable_0 =
createTx(
true, l->seq(), publicKeys[0]);
414 auto txReEnable_0 =
createTx(
false, l->seq(), publicKeys[0]);
415 auto txReEnable_1 =
createTx(
false, l->seq(), publicKeys[1]);
423 BEAST_EXPECT(good_size);
426 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
427 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
428 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[0]);
435 for (
auto i = 0; i < 256; ++i)
438 BEAST_EXPECT(good_size);
441 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
442 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
443 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[0]);
445 l = std::make_shared<Ledger>(
448 BEAST_EXPECT(l->isFlagLedger());
449 l->updateNegativeUNL();
454 BEAST_EXPECT(good_size);
457 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
458 nUnlLedgerSeq.
erase(publicKeys[0]);
462 auto txReEnable_1 =
createTx(
false, l->seq(), publicKeys[1]);
468 BEAST_EXPECT(good_size);
471 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
472 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[1]);
479 for (
auto i = 0; i < 256; ++i)
482 BEAST_EXPECT(good_size);
485 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
486 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[1]);
488 l = std::make_shared<Ledger>(
491 BEAST_EXPECT(l->isFlagLedger());
492 l->updateNegativeUNL();
500 for (
auto i = 0; i < 256; ++i)
503 l = std::make_shared<Ledger>(
506 BEAST_EXPECT(l->isFlagLedger());
507 l->updateNegativeUNL();
526 testcase(
"No negative UNL amendment");
531 auto l = std::make_shared<Ledger>(
539 for (
auto i = 0; i < 256 - 1; ++i)
541 l = std::make_shared<Ledger>(
544 BEAST_EXPECT(l->seq() == 256);
545 auto txDisable_0 =
createTx(
true, l->seq(), publicKeys[0]);
615 auto l = std::make_shared<Ledger>(
627 l = std::make_shared<Ledger>(
631 if (l->isFlagLedger())
633 l->updateNegativeUNL();
676 return std::make_shared<STValidation>(
682 v.setFieldH256(sfLedgerHash, ledger->info().hash);
683 v.setFieldU32(sfLedgerSequence, ledger->seq());
684 v.setFlag(vfFullValidation);
695 template <
class NeedVal
idation>
747 template <
typename PreVote = decltype(defaultPreVote)>
757 auto txSet = std::make_shared<SHAMap>(
761 return countTx(txSet) == expect;
772 testcase(
"Create UNLModify Tx");
779 auto txSet = std::make_shared<SHAMap>(
784 BEAST_EXPECT(
countTx(txSet) == 0);
786 BEAST_EXPECT(
countTx(txSet) == 1);
788 BEAST_EXPECT(
countTx(txSet) == 2);
796 testcase(
"Pick One Candidate");
808 BEAST_EXPECT(vote.
choose(pad_0, candidates) == n_1);
809 BEAST_EXPECT(vote.
choose(pad_f, candidates) == n_1);
810 candidates.emplace_back(2);
811 BEAST_EXPECT(vote.
choose(pad_0, candidates) == n_1);
812 BEAST_EXPECT(vote.
choose(pad_f, candidates) == n_2);
813 candidates.emplace_back(3);
814 BEAST_EXPECT(vote.
choose(pad_0, candidates) == n_1);
815 BEAST_EXPECT(vote.
choose(pad_f, candidates) == n_3);
821 testcase(
"Build Score Table");
889 BEAST_EXPECT(wrongChainSuccess);
910 for (
auto& l : wrongChain)
926 BEAST_EXPECT(scoreTable);
929 for (
auto const& [n, score] : *scoreTable)
932 BEAST_EXPECT(score == 256);
934 BEAST_EXPECT(score == 0);
962 BEAST_EXPECT(scoreTable);
965 for (
auto const& [_, score] : *scoreTable)
968 BEAST_EXPECT(score == 256);
996 auto [disableCandidates, reEnableCandidates] =
998 bool rightDisable = disableCandidates.size() == numDisable;
999 bool rightReEnable = reEnableCandidates.size() == numReEnable;
1000 return rightDisable && rightReEnable;
1006 testcase(
"Find All Candidates");
1039 vote, history.
UNLNodeIDSet, negUnl_012, goodScoreTable, 0, 3));
1047 vote, history.
UNLNodeIDSet, negUnl_012, scoreTable, 35 - 3, 0));
1055 vote, history.
UNLNodeIDSet, negUnl_012, scoreTable, 0, 0));
1060 auto scoreTable = goodScoreTable;
1061 scoreTable[*negUnl_012.
begin()] =
1064 vote, history.
UNLNodeIDSet, negUnl_012, scoreTable, 0, 2));
1069 auto scoreTable = goodScoreTable;
1075 vote, history.
UNLNodeIDSet, negUnl_012, scoreTable, 2, 3));
1085 vote, UNL_temp, negUnl_012, goodScoreTable, 0, 3));
1090 auto scoreTable = goodScoreTable;
1099 vote, UNL_temp, negUnl_012, scoreTable, 0, 2));
1113 auto scoreTable = goodScoreTable;
1119 negUnl_temp.
insert(new_1);
1120 negUnl_temp.
insert(new_2);
1122 vote, UNL_temp, negUnl_temp, scoreTable, 0, 3 + 2));
1126 auto scoreTable = goodScoreTable;
1127 scoreTable[new_1] = 0;
1128 scoreTable[new_2] = 0;
1130 vote, UNL_temp, negUnl_012, scoreTable, 0, 3));
1136 auto scoreTable = goodScoreTable;
1137 scoreTable[new_1] = 0;
1138 scoreTable[new_2] = 0;
1140 vote, UNL_temp, negUnl_012, scoreTable, 2, 3));
1148 testcase(
"Find All Candidates Combination");
1184 auto fillScoreTable =
1193 for (
auto const& k : keys)
1196 unl.emplace(nodeIDs.
back());
1197 scoreTable[nodeIDs.
back()] = score;
1200 negUnl.insert(nodeIDs[i]);
1203 for (
auto us : unlSizes)
1205 for (
auto np : nUnlPercent)
1207 for (
auto score : scores)
1213 us, us * np / 100, score, unl, negUnl, scoreTable);
1214 BEAST_EXPECT(unl.
size() == us);
1215 BEAST_EXPECT(negUnl.
size() == us * np / 100);
1216 BEAST_EXPECT(scoreTable.
size() == us);
1225 toDisable_expect = us;
1233 toReEnable_expect = us * np / 100;
1241 toReEnable_expect = us;
1250 toReEnable_expect));
1257 auto fillScoreTable =
1266 for (
auto const& k : keys)
1269 unl.emplace(nodeIDs.
back());
1273 for (
auto score : scores)
1275 scoreTable[nodeIDs[nIdx++]] = score;
1276 scoreTable[nodeIDs[nIdx++]] = score;
1278 for (; nIdx < unl_size;)
1280 scoreTable[nodeIDs[nIdx++]] = scores.back();
1283 if (nUnl_percent == 100)
1287 else if (nUnl_percent == 50)
1290 negUnl.insert(nodeIDs[i]);
1294 for (
auto us : unlSizes)
1296 for (
auto np : nUnlPercent)
1302 fillScoreTable(us, np, unl, negUnl, scoreTable);
1303 BEAST_EXPECT(unl.
size() == us);
1304 BEAST_EXPECT(negUnl.
size() == us * np / 100);
1305 BEAST_EXPECT(scoreTable.
size() == us);
1311 toDisable_expect = 4;
1315 toReEnable_expect = negUnl.
size() - 6;
1319 toReEnable_expect = negUnl.
size() - 12;
1327 toReEnable_expect));
1337 testcase(
"New Validators");
1415 testcase(
"Build Score Table Combination");
1424 {{{0, 0, 0}}, {{50, 50, 50}}, {{100, 100, 100}}, {{0, 50, 100}}}};
1426 for (
auto unlSize : unlSizes)
1431 *
this, {unlSize, 0,
false,
false, 256 + 2}};
1448 scorePattern[sp][k] == 50 && l->seq() % 2 == 0;
1449 bool add_100 = scorePattern[sp][k] == 100;
1450 bool add_me = history.
UNLNodeIDs[idx] == myId;
1451 return add_50 || add_100 || add_me;
1459 BEAST_EXPECT(scoreTable);
1466 return score == 256;
1467 if (scorePattern[sp][k] == 0)
1469 if (scorePattern[sp][k] == 50)
1470 return score == 256 / 2;
1471 if (scorePattern[sp][k] == 100)
1472 return score == 256;
1478 BEAST_EXPECT(checkScores(
1483 BEAST_EXPECT(checkScores(
1486 for (; i < unlSize; ++i)
1488 BEAST_EXPECT(checkScores(
1541 testcase(
"Do Voting");
1584 testcase(
"Do Voting");
1640 testcase(
"Do Voting");
1673 testcase(
"Do Voting");
1718 history.UNLKeySet.erase(history.UNLKeys[0]);
1719 history.UNLKeySet.erase(history.UNLKeys[1]);
1737 testcase(
"Do Voting");
1755 randomKeyPair(KeyType::ed25519).first;
1757 randomKeyPair(KeyType::ed25519).first;
1758 history.UNLKeySet.insert(extra_key_1);
1759 history.UNLKeySet.insert(extra_key_2);
1760 hash_set<NodeID> nowTrusted;
1761 nowTrusted.insert(calcNodeID(extra_key_1));
1762 nowTrusted.insert(calcNodeID(extra_key_2));
1764 history.lastLedger()->seq(), nowTrusted);
1791 randomKeyPair(KeyType::ed25519).first;
1793 randomKeyPair(KeyType::ed25519).first;
1794 history.UNLKeySet.insert(extra_key_1);
1795 history.UNLKeySet.insert(extra_key_2);
1796 hash_set<NodeID> nowTrusted;
1797 nowTrusted.insert(calcNodeID(extra_key_1));
1798 nowTrusted.insert(calcNodeID(extra_key_2));
1799 vote.newValidators(256, nowTrusted);
1817 testcase(
"Filter Validations");
1819 auto l = std::make_shared<Ledger>(
1826 return std::make_shared<STValidation>(
1832 v.setFieldH256(sfLedgerHash, l->info().hash);
1833 v.setFieldU32(sfLedgerSequence, l->seq());
1834 v.setFlag(vfFullValidation);
1845 for (
int i = 0; i < numNodes; ++i)
1853 nUnlKeys.
insert(keyPair.first);
1859 auto& local = *nUnlKeys.
begin();
1861 validators.load(local, cfgKeys, cfgPublishers);
1862 validators.updateTrusted(
1868 BEAST_EXPECT(validators.getTrustedMasterKeys().size() == numNodes);
1869 validators.setNegativeUNL(nUnlKeys);
1870 BEAST_EXPECT(validators.getNegativeUNL().size() == negUnlSize);
1873 BEAST_EXPECT(vals.
size() == numNodes);
1874 vals = validators.negativeUNLFilter(std::move(vals));
1875 BEAST_EXPECT(vals.
size() == numNodes - negUnlSize);
1894 NegativeUNLVoteRetiredValidator,
1911 bool sameSize = l->negativeUNL().size() == size;
1912 bool sameToDisable =
1913 (l->validatorToDisable() != std::nullopt) == hasToDisable;
1914 bool sameToReEnable =
1915 (l->validatorToReEnable() != std::nullopt) == hasToReEnable;
1917 return sameSize && sameToDisable && sameToReEnable;
1923 auto res =
apply(env.
app(), view, tx, ApplyFlags::tapNONE, env.
journal);
1942 if (nUnlData.size() != nUnlLedgerSeq.
size())
1945 for (
auto const& n : nUnlData)
1957 auto it = nUnlLedgerSeq.
find(pk);
1958 if (it == nUnlLedgerSeq.
end())
1960 if (it->second !=
seq)
1962 nUnlLedgerSeq.
erase(it);
1964 return nUnlLedgerSeq.
size() == 0;
1971 for (
auto i = txSet->begin(); i != txSet->end(); ++i)
1985 for (
int i = 0; i < n; ++i)
1988 Slice s(data.data(), ss);
1997 auto fill = [&](
auto& obj) {