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>(
786 BEAST_EXPECT(
countTx(txSet) == 0);
788 BEAST_EXPECT(
countTx(txSet) == 1);
790 BEAST_EXPECT(
countTx(txSet) == 2);
798 testcase(
"Pick One Candidate");
810 BEAST_EXPECT(vote.
choose(pad_0, candidates) == n_1);
811 BEAST_EXPECT(vote.
choose(pad_f, candidates) == n_1);
812 candidates.emplace_back(2);
813 BEAST_EXPECT(vote.
choose(pad_0, candidates) == n_1);
814 BEAST_EXPECT(vote.
choose(pad_f, candidates) == n_2);
815 candidates.emplace_back(3);
816 BEAST_EXPECT(vote.
choose(pad_0, candidates) == n_1);
817 BEAST_EXPECT(vote.
choose(pad_f, candidates) == n_3);
823 testcase(
"Build Score Table");
891 BEAST_EXPECT(wrongChainSuccess);
912 for (
auto& l : wrongChain)
928 BEAST_EXPECT(scoreTable);
931 for (
auto const& [n, score] : *scoreTable)
934 BEAST_EXPECT(score == 256);
936 BEAST_EXPECT(score == 0);
964 BEAST_EXPECT(scoreTable);
967 for (
auto const& [_, score] : *scoreTable)
970 BEAST_EXPECT(score == 256);
998 auto [disableCandidates, reEnableCandidates] =
1000 bool rightDisable = disableCandidates.size() == numDisable;
1001 bool rightReEnable = reEnableCandidates.size() == numReEnable;
1002 return rightDisable && rightReEnable;
1008 testcase(
"Find All Candidates");
1041 vote, history.
UNLNodeIDSet, negUnl_012, goodScoreTable, 0, 3));
1049 vote, history.
UNLNodeIDSet, negUnl_012, scoreTable, 35 - 3, 0));
1057 vote, history.
UNLNodeIDSet, negUnl_012, scoreTable, 0, 0));
1062 auto scoreTable = goodScoreTable;
1063 scoreTable[*negUnl_012.
begin()] =
1066 vote, history.
UNLNodeIDSet, negUnl_012, scoreTable, 0, 2));
1071 auto scoreTable = goodScoreTable;
1077 vote, history.
UNLNodeIDSet, negUnl_012, scoreTable, 2, 3));
1087 vote, UNL_temp, negUnl_012, goodScoreTable, 0, 3));
1092 auto scoreTable = goodScoreTable;
1101 vote, UNL_temp, negUnl_012, scoreTable, 0, 2));
1115 auto scoreTable = goodScoreTable;
1121 negUnl_temp.
insert(new_1);
1122 negUnl_temp.
insert(new_2);
1124 vote, UNL_temp, negUnl_temp, scoreTable, 0, 3 + 2));
1128 auto scoreTable = goodScoreTable;
1129 scoreTable[new_1] = 0;
1130 scoreTable[new_2] = 0;
1132 vote, UNL_temp, negUnl_012, scoreTable, 0, 3));
1138 auto scoreTable = goodScoreTable;
1139 scoreTable[new_1] = 0;
1140 scoreTable[new_2] = 0;
1142 vote, UNL_temp, negUnl_012, scoreTable, 2, 3));
1150 testcase(
"Find All Candidates Combination");
1186 auto fillScoreTable =
1195 for (
auto const& k : keys)
1198 unl.emplace(nodeIDs.
back());
1199 scoreTable[nodeIDs.
back()] = score;
1202 negUnl.insert(nodeIDs[i]);
1205 for (
auto us : unlSizes)
1207 for (
auto np : nUnlPercent)
1209 for (
auto score : scores)
1215 us, us * np / 100, score, unl, negUnl, scoreTable);
1216 BEAST_EXPECT(unl.
size() == us);
1217 BEAST_EXPECT(negUnl.
size() == us * np / 100);
1218 BEAST_EXPECT(scoreTable.
size() == us);
1227 toDisable_expect = us;
1235 toReEnable_expect = us * np / 100;
1243 toReEnable_expect = us;
1252 toReEnable_expect));
1259 auto fillScoreTable =
1268 for (
auto const& k : keys)
1271 unl.emplace(nodeIDs.
back());
1275 for (
auto score : scores)
1277 scoreTable[nodeIDs[nIdx++]] = score;
1278 scoreTable[nodeIDs[nIdx++]] = score;
1280 for (; nIdx < unl_size;)
1282 scoreTable[nodeIDs[nIdx++]] = scores.back();
1285 if (nUnl_percent == 100)
1289 else if (nUnl_percent == 50)
1292 negUnl.insert(nodeIDs[i]);
1296 for (
auto us : unlSizes)
1298 for (
auto np : nUnlPercent)
1304 fillScoreTable(us, np, unl, negUnl, scoreTable);
1305 BEAST_EXPECT(unl.
size() == us);
1306 BEAST_EXPECT(negUnl.
size() == us * np / 100);
1307 BEAST_EXPECT(scoreTable.
size() == us);
1313 toDisable_expect = 4;
1317 toReEnable_expect = negUnl.
size() - 6;
1321 toReEnable_expect = negUnl.
size() - 12;
1329 toReEnable_expect));
1339 testcase(
"New Validators");
1417 testcase(
"Build Score Table Combination");
1426 {{{0, 0, 0}}, {{50, 50, 50}}, {{100, 100, 100}}, {{0, 50, 100}}}};
1428 for (
auto unlSize : unlSizes)
1433 *
this, {unlSize, 0,
false,
false, 256 + 2}};
1450 scorePattern[sp][k] == 50 && l->seq() % 2 == 0;
1451 bool add_100 = scorePattern[sp][k] == 100;
1452 bool add_me = history.
UNLNodeIDs[idx] == myId;
1453 return add_50 || add_100 || add_me;
1461 BEAST_EXPECT(scoreTable);
1468 return score == 256;
1469 if (scorePattern[sp][k] == 0)
1471 if (scorePattern[sp][k] == 50)
1472 return score == 256 / 2;
1473 if (scorePattern[sp][k] == 100)
1474 return score == 256;
1480 BEAST_EXPECT(checkScores(
1485 BEAST_EXPECT(checkScores(
1488 for (; i < unlSize; ++i)
1490 BEAST_EXPECT(checkScores(
1543 testcase(
"Do Voting");
1586 testcase(
"Do Voting");
1642 testcase(
"Do Voting");
1675 testcase(
"Do Voting");
1720 history.UNLKeySet.erase(history.UNLKeys[0]);
1721 history.UNLKeySet.erase(history.UNLKeys[1]);
1739 testcase(
"Do Voting");
1757 randomKeyPair(KeyType::ed25519).first;
1759 randomKeyPair(KeyType::ed25519).first;
1760 history.UNLKeySet.insert(extra_key_1);
1761 history.UNLKeySet.insert(extra_key_2);
1762 hash_set<NodeID> nowTrusted;
1763 nowTrusted.insert(calcNodeID(extra_key_1));
1764 nowTrusted.insert(calcNodeID(extra_key_2));
1766 history.lastLedger()->seq(), nowTrusted);
1793 randomKeyPair(KeyType::ed25519).first;
1795 randomKeyPair(KeyType::ed25519).first;
1796 history.UNLKeySet.insert(extra_key_1);
1797 history.UNLKeySet.insert(extra_key_2);
1798 hash_set<NodeID> nowTrusted;
1799 nowTrusted.insert(calcNodeID(extra_key_1));
1800 nowTrusted.insert(calcNodeID(extra_key_2));
1801 vote.newValidators(256, nowTrusted);
1819 testcase(
"Filter Validations");
1821 auto l = std::make_shared<Ledger>(
1828 return std::make_shared<STValidation>(
1834 v.setFieldH256(sfLedgerHash, l->info().hash);
1835 v.setFieldU32(sfLedgerSequence, l->seq());
1836 v.setFlag(vfFullValidation);
1847 for (
int i = 0; i < numNodes; ++i)
1855 nUnlKeys.
insert(keyPair.first);
1861 auto& local = *nUnlKeys.
begin();
1863 validators.load(local, cfgKeys, cfgPublishers);
1864 validators.updateTrusted(
1870 BEAST_EXPECT(validators.getTrustedMasterKeys().size() == numNodes);
1871 validators.setNegativeUNL(nUnlKeys);
1872 BEAST_EXPECT(validators.getNegativeUNL().size() == negUnlSize);
1875 BEAST_EXPECT(vals.
size() == numNodes);
1876 vals = validators.negativeUNLFilter(std::move(vals));
1877 BEAST_EXPECT(vals.
size() == numNodes - negUnlSize);
1896 NegativeUNLVoteRetiredValidator,
1913 bool sameSize = l->negativeUNL().size() == size;
1914 bool sameToDisable =
1915 (l->validatorToDisable() != std::nullopt) == hasToDisable;
1916 bool sameToReEnable =
1917 (l->validatorToReEnable() != std::nullopt) == hasToReEnable;
1919 return sameSize && sameToDisable && sameToReEnable;
1925 auto res =
apply(env.
app(), view, tx, ApplyFlags::tapNONE, env.
journal);
1944 if (nUnlData.size() != nUnlLedgerSeq.
size())
1947 for (
auto const& n : nUnlData)
1959 auto it = nUnlLedgerSeq.
find(pk);
1960 if (it == nUnlLedgerSeq.
end())
1962 if (it->second !=
seq)
1964 nUnlLedgerSeq.
erase(it);
1966 return nUnlLedgerSeq.
size() == 0;
1973 for (
auto i = txSet->begin(); i != txSet->end(); ++i)
1987 for (
int i = 0; i < n; ++i)
1990 Slice s(data.data(), ss);
1999 auto fill = [&](
auto& obj) {