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>
28 #include <ripple/rpc/impl/GRPCHelpers.h>
87 hash_map<PublicKey, std::uint32_t> nUnlLedgerSeq);
228 testcase(
"Create UNLModify Tx and apply to ledgers");
233 auto l = std::make_shared<Ledger>(
246 l = std::make_shared<Ledger>(
249 auto txDisable_0 =
createTx(
true, l->seq(), publicKeys[0]);
250 auto txReEnable_1 =
createTx(
false, l->seq(), publicKeys[1]);
262 for (
auto i = 0; i < 256 - 2; ++i)
264 l = std::make_shared<Ledger>(
267 BEAST_EXPECT(l->isFlagLedger());
268 l->updateNegativeUNL();
270 auto txDisable_0 =
createTx(
true, l->seq(), publicKeys[0]);
271 auto txDisable_1 =
createTx(
true, l->seq(), publicKeys[1]);
272 auto txReEnable_2 =
createTx(
false, l->seq(), publicKeys[2]);
282 BEAST_EXPECT(good_size);
285 BEAST_EXPECT(l->validatorToDisable() == publicKeys[0]);
287 uint256 txID = txDisable_0.getTransactionID();
288 BEAST_EXPECT(l->txExists(txID));
294 for (
auto i = 0; i < 256; ++i)
297 BEAST_EXPECT(good_size);
299 BEAST_EXPECT(l->validatorToDisable() == publicKeys[0]);
300 l = std::make_shared<Ledger>(
303 BEAST_EXPECT(l->isFlagLedger());
304 l->updateNegativeUNL();
309 BEAST_EXPECT(good_size);
312 BEAST_EXPECT(*(l->negativeUNL().begin()) == publicKeys[0]);
313 nUnlLedgerSeq.
emplace(publicKeys[0], l->seq());
316 auto txDisable_0 =
createTx(
true, l->seq(), publicKeys[0]);
317 auto txDisable_1 =
createTx(
true, l->seq(), publicKeys[1]);
318 auto txReEnable_0 =
createTx(
false, l->seq(), publicKeys[0]);
319 auto txReEnable_1 =
createTx(
false, l->seq(), publicKeys[1]);
320 auto txReEnable_2 =
createTx(
false, l->seq(), publicKeys[2]);
330 BEAST_EXPECT(good_size);
333 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
334 BEAST_EXPECT(l->validatorToDisable() == publicKeys[1]);
335 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[0]);
343 for (
auto i = 0; i < 256; ++i)
346 BEAST_EXPECT(good_size);
349 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
350 BEAST_EXPECT(l->validatorToDisable() == publicKeys[1]);
351 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[0]);
353 l = std::make_shared<Ledger>(
356 BEAST_EXPECT(l->isFlagLedger());
357 l->updateNegativeUNL();
362 BEAST_EXPECT(good_size);
365 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
368 auto txDisable_0 =
createTx(
true, l->seq(), publicKeys[0]);
374 BEAST_EXPECT(good_size);
377 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
378 BEAST_EXPECT(l->validatorToDisable() == publicKeys[0]);
379 nUnlLedgerSeq.
emplace(publicKeys[1], l->seq());
380 nUnlLedgerSeq.
erase(publicKeys[0]);
387 for (
auto i = 0; i < 256; ++i)
390 BEAST_EXPECT(good_size);
393 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
394 BEAST_EXPECT(l->validatorToDisable() == publicKeys[0]);
396 l = std::make_shared<Ledger>(
399 BEAST_EXPECT(l->isFlagLedger());
400 l->updateNegativeUNL();
405 BEAST_EXPECT(good_size);
408 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
409 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
410 nUnlLedgerSeq.
emplace(publicKeys[0], l->seq());
414 auto txDisable_0 =
createTx(
true, l->seq(), publicKeys[0]);
415 auto txReEnable_0 =
createTx(
false, l->seq(), publicKeys[0]);
416 auto txReEnable_1 =
createTx(
false, l->seq(), publicKeys[1]);
424 BEAST_EXPECT(good_size);
427 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
428 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
429 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[0]);
436 for (
auto i = 0; i < 256; ++i)
439 BEAST_EXPECT(good_size);
442 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
443 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
444 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[0]);
446 l = std::make_shared<Ledger>(
449 BEAST_EXPECT(l->isFlagLedger());
450 l->updateNegativeUNL();
455 BEAST_EXPECT(good_size);
458 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
459 nUnlLedgerSeq.
erase(publicKeys[0]);
463 auto txReEnable_1 =
createTx(
false, l->seq(), publicKeys[1]);
469 BEAST_EXPECT(good_size);
472 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
473 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[1]);
480 for (
auto i = 0; i < 256; ++i)
483 BEAST_EXPECT(good_size);
486 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
487 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[1]);
489 l = std::make_shared<Ledger>(
492 BEAST_EXPECT(l->isFlagLedger());
493 l->updateNegativeUNL();
501 for (
auto i = 0; i < 256; ++i)
504 l = std::make_shared<Ledger>(
507 BEAST_EXPECT(l->isFlagLedger());
508 l->updateNegativeUNL();
527 testcase(
"No negative UNL amendment");
532 auto l = std::make_shared<Ledger>(
540 for (
auto i = 0; i < 256 - 1; ++i)
542 l = std::make_shared<Ledger>(
545 BEAST_EXPECT(l->seq() == 256);
546 auto txDisable_0 =
createTx(
true, l->seq(), publicKeys[0]);
616 auto l = std::make_shared<Ledger>(
628 l = std::make_shared<Ledger>(
632 if (l->isFlagLedger())
634 l->updateNegativeUNL();
677 return std::make_shared<STValidation>(
683 v.setFieldH256(sfLedgerHash, ledger->info().hash);
684 v.setFieldU32(sfLedgerSequence, ledger->seq());
685 v.setFlag(vfFullValidation);
696 template <
class NeedVal
idation>
748 template <
typename PreVote = decltype(defaultPreVote)>
758 auto txSet = std::make_shared<SHAMap>(
762 return countTx(txSet) == expect;
773 testcase(
"Create UNLModify Tx");
780 auto txSet = std::make_shared<SHAMap>(
785 BEAST_EXPECT(
countTx(txSet) == 0);
787 BEAST_EXPECT(
countTx(txSet) == 1);
789 BEAST_EXPECT(
countTx(txSet) == 2);
797 testcase(
"Pick One Candidate");
809 BEAST_EXPECT(vote.
choose(pad_0, candidates) == n_1);
810 BEAST_EXPECT(vote.
choose(pad_f, candidates) == n_1);
811 candidates.emplace_back(2);
812 BEAST_EXPECT(vote.
choose(pad_0, candidates) == n_1);
813 BEAST_EXPECT(vote.
choose(pad_f, candidates) == n_2);
814 candidates.emplace_back(3);
815 BEAST_EXPECT(vote.
choose(pad_0, candidates) == n_1);
816 BEAST_EXPECT(vote.
choose(pad_f, candidates) == n_3);
822 testcase(
"Build Score Table");
890 BEAST_EXPECT(wrongChainSuccess);
911 for (
auto& l : wrongChain)
927 BEAST_EXPECT(scoreTable);
930 for (
auto const& [n, score] : *scoreTable)
933 BEAST_EXPECT(score == 256);
935 BEAST_EXPECT(score == 0);
963 BEAST_EXPECT(scoreTable);
966 for (
auto const& [_, score] : *scoreTable)
969 BEAST_EXPECT(score == 256);
997 auto [disableCandidates, reEnableCandidates] =
999 bool rightDisable = disableCandidates.size() == numDisable;
1000 bool rightReEnable = reEnableCandidates.size() == numReEnable;
1001 return rightDisable && rightReEnable;
1007 testcase(
"Find All Candidates");
1040 vote, history.
UNLNodeIDSet, negUnl_012, goodScoreTable, 0, 3));
1048 vote, history.
UNLNodeIDSet, negUnl_012, scoreTable, 35 - 3, 0));
1056 vote, history.
UNLNodeIDSet, negUnl_012, scoreTable, 0, 0));
1061 auto scoreTable = goodScoreTable;
1062 scoreTable[*negUnl_012.
begin()] =
1065 vote, history.
UNLNodeIDSet, negUnl_012, scoreTable, 0, 2));
1070 auto scoreTable = goodScoreTable;
1076 vote, history.
UNLNodeIDSet, negUnl_012, scoreTable, 2, 3));
1086 vote, UNL_temp, negUnl_012, goodScoreTable, 0, 3));
1091 auto scoreTable = goodScoreTable;
1100 vote, UNL_temp, negUnl_012, scoreTable, 0, 2));
1114 auto scoreTable = goodScoreTable;
1120 negUnl_temp.
insert(new_1);
1121 negUnl_temp.
insert(new_2);
1123 vote, UNL_temp, negUnl_temp, scoreTable, 0, 3 + 2));
1127 auto scoreTable = goodScoreTable;
1128 scoreTable[new_1] = 0;
1129 scoreTable[new_2] = 0;
1131 vote, UNL_temp, negUnl_012, scoreTable, 0, 3));
1137 auto scoreTable = goodScoreTable;
1138 scoreTable[new_1] = 0;
1139 scoreTable[new_2] = 0;
1141 vote, UNL_temp, negUnl_012, scoreTable, 2, 3));
1149 testcase(
"Find All Candidates Combination");
1185 auto fillScoreTable =
1194 for (
auto const& k : keys)
1197 unl.emplace(nodeIDs.
back());
1198 scoreTable[nodeIDs.
back()] = score;
1201 negUnl.insert(nodeIDs[i]);
1204 for (
auto us : unlSizes)
1206 for (
auto np : nUnlPercent)
1208 for (
auto score : scores)
1214 us, us * np / 100, score, unl, negUnl, scoreTable);
1215 BEAST_EXPECT(unl.
size() == us);
1216 BEAST_EXPECT(negUnl.
size() == us * np / 100);
1217 BEAST_EXPECT(scoreTable.
size() == us);
1226 toDisable_expect = us;
1234 toReEnable_expect = us * np / 100;
1242 toReEnable_expect = us;
1251 toReEnable_expect));
1258 auto fillScoreTable =
1267 for (
auto const& k : keys)
1270 unl.emplace(nodeIDs.
back());
1274 for (
auto score : scores)
1276 scoreTable[nodeIDs[nIdx++]] = score;
1277 scoreTable[nodeIDs[nIdx++]] = score;
1279 for (; nIdx < unl_size;)
1281 scoreTable[nodeIDs[nIdx++]] = scores.back();
1284 if (nUnl_percent == 100)
1288 else if (nUnl_percent == 50)
1291 negUnl.insert(nodeIDs[i]);
1295 for (
auto us : unlSizes)
1297 for (
auto np : nUnlPercent)
1303 fillScoreTable(us, np, unl, negUnl, scoreTable);
1304 BEAST_EXPECT(unl.
size() == us);
1305 BEAST_EXPECT(negUnl.
size() == us * np / 100);
1306 BEAST_EXPECT(scoreTable.
size() == us);
1312 toDisable_expect = 4;
1316 toReEnable_expect = negUnl.
size() - 6;
1320 toReEnable_expect = negUnl.
size() - 12;
1328 toReEnable_expect));
1338 testcase(
"New Validators");
1416 testcase(
"Build Score Table Combination");
1425 {{{0, 0, 0}}, {{50, 50, 50}}, {{100, 100, 100}}, {{0, 50, 100}}}};
1427 for (
auto unlSize : unlSizes)
1432 *
this, {unlSize, 0,
false,
false, 256 + 2}};
1449 scorePattern[sp][k] == 50 && l->seq() % 2 == 0;
1450 bool add_100 = scorePattern[sp][k] == 100;
1451 bool add_me = history.
UNLNodeIDs[idx] == myId;
1452 return add_50 || add_100 || add_me;
1460 BEAST_EXPECT(scoreTable);
1467 return score == 256;
1468 if (scorePattern[sp][k] == 0)
1470 if (scorePattern[sp][k] == 50)
1471 return score == 256 / 2;
1472 if (scorePattern[sp][k] == 100)
1473 return score == 256;
1479 BEAST_EXPECT(checkScores(
1484 BEAST_EXPECT(checkScores(
1487 for (; i < unlSize; ++i)
1489 BEAST_EXPECT(checkScores(
1542 testcase(
"Do Voting");
1585 testcase(
"Do Voting");
1641 testcase(
"Do Voting");
1674 testcase(
"Do Voting");
1719 history.UNLKeySet.erase(history.UNLKeys[0]);
1720 history.UNLKeySet.erase(history.UNLKeys[1]);
1738 testcase(
"Do Voting");
1756 randomKeyPair(KeyType::ed25519).first;
1758 randomKeyPair(KeyType::ed25519).first;
1759 history.UNLKeySet.insert(extra_key_1);
1760 history.UNLKeySet.insert(extra_key_2);
1761 hash_set<NodeID> nowTrusted;
1762 nowTrusted.insert(calcNodeID(extra_key_1));
1763 nowTrusted.insert(calcNodeID(extra_key_2));
1765 history.lastLedger()->seq(), nowTrusted);
1792 randomKeyPair(KeyType::ed25519).first;
1794 randomKeyPair(KeyType::ed25519).first;
1795 history.UNLKeySet.insert(extra_key_1);
1796 history.UNLKeySet.insert(extra_key_2);
1797 hash_set<NodeID> nowTrusted;
1798 nowTrusted.insert(calcNodeID(extra_key_1));
1799 nowTrusted.insert(calcNodeID(extra_key_2));
1800 vote.newValidators(256, nowTrusted);
1818 testcase(
"Filter Validations");
1820 auto l = std::make_shared<Ledger>(
1827 return std::make_shared<STValidation>(
1833 v.setFieldH256(sfLedgerHash, l->info().hash);
1834 v.setFieldU32(sfLedgerSequence, l->seq());
1835 v.setFlag(vfFullValidation);
1846 for (
int i = 0; i < numNodes; ++i)
1854 nUnlKeys.
insert(keyPair.first);
1860 auto& local = *nUnlKeys.
begin();
1862 validators.load(local, cfgKeys, cfgPublishers);
1863 validators.updateTrusted(activeValidators);
1864 BEAST_EXPECT(validators.getTrustedMasterKeys().size() == numNodes);
1865 validators.setNegativeUNL(nUnlKeys);
1866 BEAST_EXPECT(validators.getNegativeUNL().size() == negUnlSize);
1869 BEAST_EXPECT(vals.
size() == numNodes);
1870 vals = validators.negativeUNLFilter(std::move(vals));
1871 BEAST_EXPECT(vals.
size() == numNodes - negUnlSize);
1887 const char* bytes =
reinterpret_cast<const char*
>(data.data());
1888 return {bytes, data.size()};
1894 testcase(
"gRPC test");
1896 auto gRpcTest = [
this](
1899 bool hasToReEnable) ->
bool {
1901 *
this, {20, negUnlSize, hasToDisable, hasToReEnable, {}}};
1905 auto const& negUnlObject =
1907 if (!negUnlSize && !hasToDisable && !hasToReEnable && !negUnlObject)
1912 org::xrpl::rpc::v1::NegativeUNL to;
1914 if (!to.has_flags() ||
1915 to.flags().value() != negUnlObject->getFlags())
1918 bool goodSize = to.disabled_validators_size() == negUnlSize &&
1919 to.has_validator_to_disable() == hasToDisable &&
1920 to.has_validator_to_re_enable() == hasToReEnable;
1928 auto const& nUnlData =
1930 if (nUnlData.size() != negUnlSize)
1933 for (
auto const& n : nUnlData)
1939 if (!to.disabled_validators(idx).has_ledger_sequence() ||
1940 !to.disabled_validators(idx).has_public_key())
1943 if (to.disabled_validators(idx).public_key().value() !=
1947 if (to.disabled_validators(idx).ledger_sequence().value() !=
1959 if (to.validator_to_disable().value() !=
1969 if (to.validator_to_re_enable().value() !=
1978 BEAST_EXPECT(gRpcTest(0,
false,
false));
1979 BEAST_EXPECT(gRpcTest(2,
true,
true));
1998 NegativeUNLVoteRetiredValidator,
2016 bool sameSize = l->negativeUNL().size() == size;
2017 bool sameToDisable =
2018 (l->validatorToDisable() != boost::none) == hasToDisable;
2019 bool sameToReEnable =
2020 (l->validatorToReEnable() != boost::none) == hasToReEnable;
2022 return sameSize && sameToDisable && sameToReEnable;
2028 auto res =
apply(env.
app(), view, tx, ApplyFlags::tapNONE, env.
journal);
2047 if (nUnlData.size() != nUnlLedgerSeq.
size())
2050 for (
auto const& n : nUnlData)
2062 auto it = nUnlLedgerSeq.
find(pk);
2063 if (it == nUnlLedgerSeq.
end())
2065 if (it->second !=
seq)
2067 nUnlLedgerSeq.
erase(it);
2069 return nUnlLedgerSeq.
size() == 0;
2076 for (
auto i = txSet->begin(); i != txSet->end(); ++i)
2090 for (
int i = 0; i < n; ++i)
2093 Slice s(data.data(), ss);
2102 auto fill = [&](
auto& obj) {