xrpld
Loading...
Searching...
No Matches
NegativeUNL_test.cpp
1
2#include <test/jtx/Env.h>
3
4#include <xrpld/app/consensus/RCLValidations.h>
5#include <xrpld/app/misc/NegativeUNLVote.h>
6#include <xrpld/app/misc/ValidatorList.h>
7
8#include <xrpl/basics/Slice.h>
9#include <xrpl/basics/UnorderedContainers.h>
10#include <xrpl/basics/base_uint.h>
11#include <xrpl/beast/unit_test/suite.h>
12#include <xrpl/ledger/ApplyView.h>
13#include <xrpl/ledger/Ledger.h>
14#include <xrpl/ledger/OpenView.h>
15#include <xrpl/protocol/Indexes.h>
16#include <xrpl/protocol/KeyType.h>
17#include <xrpl/protocol/Protocol.h>
18#include <xrpl/protocol/PublicKey.h>
19#include <xrpl/protocol/SField.h>
20#include <xrpl/protocol/STTx.h>
21#include <xrpl/protocol/STValidation.h>
22#include <xrpl/protocol/SecretKey.h>
23#include <xrpl/protocol/TER.h>
24#include <xrpl/protocol/TxFormats.h>
25#include <xrpl/protocol/UintTypes.h>
26#include <xrpl/protocol/tokens.h>
27#include <xrpl/shamap/SHAMapMissingNode.h>
28#include <xrpl/tx/apply.h>
29
30#include <array>
31#include <cassert>
32#include <cstddef>
33#include <cstdint>
34#include <memory>
35#include <optional>
36#include <utility>
37#include <vector>
38
39namespace xrpl::test {
40
41/*
42 * This file implements the following negative UNL related tests:
43 * -- test filling and applying ttUNL_MODIFY Tx and ledger update
44 * -- test the NegativeUNLVote class. The test cases are split to multiple
45 * test classes to allow parallel execution.
46 * -- test the negativeUNLFilter function
47 *
48 * Other negative UNL related tests such as ValidatorList and RPC related ones
49 * are put in their existing unit test files.
50 */
51
62bool
64 std::shared_ptr<Ledger const> const& l,
65 size_t size,
66 bool hasToDisable,
67 bool hasToReEnable);
68
78bool
79applyAndTestResult(jtx::Env& env, OpenView& view, STTx const& tx, bool pass);
80
90bool
92 std::shared_ptr<Ledger const> const& l,
94
101std::size_t
102countTx(std::shared_ptr<SHAMap> const& txSet);
103
110std::vector<PublicKey>
111createPublicKeys(std::size_t n);
112
121STTx
122createTx(bool disabling, LedgerIndex seq, PublicKey const& txKey);
123
125{
133 void
135 {
136 /*
137 * test cases:
138 *
139 * (1) the ledger after genesis
140 * -- cannot apply Disable Tx
141 * -- cannot apply ReEnable Tx
142 * -- nUNL empty
143 * -- no ToDisable
144 * -- no ToReEnable
145 *
146 * (2) a flag ledger
147 * -- apply an Disable Tx
148 * -- cannot apply the second Disable Tx
149 * -- cannot apply a ReEnable Tx
150 * -- nUNL empty
151 * -- has ToDisable with right nodeId
152 * -- no ToReEnable
153 * ++ extra test: first Disable Tx in ledger TxSet
154 *
155 * (3) ledgers before the next flag ledger
156 * -- nUNL empty
157 * -- has ToDisable with right nodeId
158 * -- no ToReEnable
159 *
160 * (4) next flag ledger
161 * -- nUNL size == 1, with right nodeId
162 * -- no ToDisable
163 * -- no ToReEnable
164 * -- cannot apply an Disable Tx with nodeId already in nUNL
165 * -- apply an Disable Tx with different nodeId
166 * -- cannot apply a ReEnable Tx with the same NodeId as Add
167 * -- cannot apply a ReEnable Tx with a NodeId not in nUNL
168 * -- apply a ReEnable Tx with a nodeId already in nUNL
169 * -- has ToDisable with right nodeId
170 * -- has ToReEnable with right nodeId
171 * -- nUNL size still 1, right nodeId
172 *
173 * (5) ledgers before the next flag ledger
174 * -- nUNL size == 1, right nodeId
175 * -- has ToDisable with right nodeId
176 * -- has ToReEnable with right nodeId
177 *
178 * (6) next flag ledger
179 * -- nUNL size == 1, different nodeId
180 * -- no ToDisable
181 * -- no ToReEnable
182 * -- apply an Disable Tx with different nodeId
183 * -- nUNL size still 1, right nodeId
184 * -- has ToDisable with right nodeId
185 * -- no ToReEnable
186 *
187 * (7) ledgers before the next flag ledger
188 * -- nUNL size still 1, right nodeId
189 * -- has ToDisable with right nodeId
190 * -- no ToReEnable
191 *
192 * (8) next flag ledger
193 * -- nUNL size == 2
194 * -- apply a ReEnable Tx
195 * -- cannot apply second ReEnable Tx, even with right nodeId
196 * -- cannot apply an Disable Tx with the same NodeId as Remove
197 * -- nUNL size == 2
198 * -- no ToDisable
199 * -- has ToReEnable with right nodeId
200 *
201 * (9) ledgers before the next flag ledger
202 * -- nUNL size == 2
203 * -- no ToDisable
204 * -- has ToReEnable with right nodeId
205 *
206 * (10) next flag ledger
207 * -- nUNL size == 1
208 * -- apply a ReEnable Tx
209 * -- nUNL size == 1
210 * -- no ToDisable
211 * -- has ToReEnable with right nodeId
212 *
213 * (11) ledgers before the next flag ledger
214 * -- nUNL size == 1
215 * -- no ToDisable
216 * -- has ToReEnable with right nodeId
217 *
218 * (12) next flag ledger
219 * -- nUNL size == 0
220 * -- no ToDisable
221 * -- no ToReEnable
222 *
223 * (13) ledgers before the next flag ledger
224 * -- nUNL size == 0
225 * -- no ToDisable
226 * -- no ToReEnable
227 *
228 * (14) next flag ledger
229 * -- nUNL size == 0
230 * -- no ToDisable
231 * -- no ToReEnable
232 */
233
234 testcase("Create UNLModify Tx and apply to ledgers");
235
236 jtx::Env env(*this, jtx::testableAmendments());
238 // genesis ledger
241 Rules{env.app().config().features},
242 env.app().config().fees.toFees(),
244 env.app().getNodeFamily());
245
246 // Record the public keys and ledger sequences of expected negative UNL
247 // validators when we build the ledger history
249
250 {
251 //(1) the ledger after genesis, not a flag ledger
253
254 auto const txDisable0 = createTx(true, l->seq(), publicKeys[0]);
255 auto const txReEnable1 = createTx(false, l->seq(), publicKeys[1]);
256
257 OpenView accum(&*l);
258 BEAST_EXPECT(applyAndTestResult(env, accum, txDisable0, false));
259 BEAST_EXPECT(applyAndTestResult(env, accum, txReEnable1, false));
260 accum.apply(*l);
261 BEAST_EXPECT(negUnlSizeTest(l, 0, false, false));
262 }
263
264 {
265 //(2) a flag ledger
266 // generate more ledgers
267 for (auto i = 0; i < 256 - 2; ++i)
268 {
270 }
271 BEAST_EXPECT(l->isFlagLedger());
272 l->updateNegativeUNL();
273
274 auto const txDisable0 = createTx(true, l->seq(), publicKeys[0]);
275 auto const txDisable1 = createTx(true, l->seq(), publicKeys[1]);
276 auto const txReEnable2 = createTx(false, l->seq(), publicKeys[2]);
277
278 // can apply 1 and only 1 ToDisable Tx,
279 // cannot apply ToReEnable Tx, since negative UNL is empty
280 OpenView accum(&*l);
281 BEAST_EXPECT(applyAndTestResult(env, accum, txDisable0, true));
282 BEAST_EXPECT(applyAndTestResult(env, accum, txDisable1, false));
283 BEAST_EXPECT(applyAndTestResult(env, accum, txReEnable2, false));
284 accum.apply(*l);
285 auto const goodSize = negUnlSizeTest(l, 0, true, false);
286 BEAST_EXPECT(goodSize);
287 if (goodSize)
288 {
289 BEAST_EXPECT(l->validatorToDisable() == publicKeys[0]);
290 //++ first ToDisable Tx in ledger's TxSet
291 UInt256 const txID = txDisable0.getTransactionID();
292 BEAST_EXPECT(l->txExists(txID));
293 }
294 }
295
296 {
297 //(3) ledgers before the next flag ledger
298 for (auto i = 0; i < 256; ++i)
299 {
300 auto const goodSize = negUnlSizeTest(l, 0, true, false);
301 BEAST_EXPECT(goodSize);
302 if (goodSize)
303 BEAST_EXPECT(l->validatorToDisable() == publicKeys[0]);
305 }
306 BEAST_EXPECT(l->isFlagLedger());
307 l->updateNegativeUNL();
308
309 //(4) next flag ledger
310 // test if the ledger updated correctly
311 auto goodSize = negUnlSizeTest(l, 1, false, false);
312 BEAST_EXPECT(goodSize);
313 if (goodSize)
314 {
315 BEAST_EXPECT(*(l->negativeUNL().begin()) == publicKeys[0]);
316 nUnlLedgerSeq.emplace(publicKeys[0], l->seq());
317 }
318
319 auto const txDisable0 = createTx(true, l->seq(), publicKeys[0]);
320 auto const txDisable1 = createTx(true, l->seq(), publicKeys[1]);
321 auto const txReEnable0 = createTx(false, l->seq(), publicKeys[0]);
322 auto const txReEnable1 = createTx(false, l->seq(), publicKeys[1]);
323 auto const txReEnable2 = createTx(false, l->seq(), publicKeys[2]);
324
325 OpenView accum(&*l);
326 BEAST_EXPECT(applyAndTestResult(env, accum, txDisable0, false));
327 BEAST_EXPECT(applyAndTestResult(env, accum, txDisable1, true));
328 BEAST_EXPECT(applyAndTestResult(env, accum, txReEnable1, false));
329 BEAST_EXPECT(applyAndTestResult(env, accum, txReEnable2, false));
330 BEAST_EXPECT(applyAndTestResult(env, accum, txReEnable0, true));
331 accum.apply(*l);
332 goodSize = negUnlSizeTest(l, 1, true, true);
333 BEAST_EXPECT(goodSize);
334 if (goodSize)
335 {
336 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
337 BEAST_EXPECT(l->validatorToDisable() == publicKeys[1]);
338 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[0]);
339 // test sfFirstLedgerSequence
340 BEAST_EXPECT(verifyPubKeyAndSeq(l, nUnlLedgerSeq));
341 }
342 }
343
344 {
345 //(5) ledgers before the next flag ledger
346 for (auto i = 0; i < 256; ++i)
347 {
348 auto const goodSize = negUnlSizeTest(l, 1, true, true);
349 BEAST_EXPECT(goodSize);
350 if (goodSize)
351 {
352 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
353 BEAST_EXPECT(l->validatorToDisable() == publicKeys[1]);
354 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[0]);
355 }
357 }
358 BEAST_EXPECT(l->isFlagLedger());
359 l->updateNegativeUNL();
360
361 //(6) next flag ledger
362 // test if the ledger updated correctly
363 auto goodSize = negUnlSizeTest(l, 1, false, false);
364 BEAST_EXPECT(goodSize);
365 if (goodSize)
366 {
367 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
368 }
369
370 auto const txDisable0 = createTx(true, l->seq(), publicKeys[0]);
371
372 OpenView accum(&*l);
373 BEAST_EXPECT(applyAndTestResult(env, accum, txDisable0, true));
374 accum.apply(*l);
375 goodSize = negUnlSizeTest(l, 1, true, false);
376 BEAST_EXPECT(goodSize);
377 if (goodSize)
378 {
379 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
380 BEAST_EXPECT(l->validatorToDisable() == publicKeys[0]);
381 nUnlLedgerSeq.emplace(publicKeys[1], l->seq());
382 nUnlLedgerSeq.erase(publicKeys[0]);
383 BEAST_EXPECT(verifyPubKeyAndSeq(l, nUnlLedgerSeq));
384 }
385 }
386
387 {
388 //(7) ledgers before the next flag ledger
389 for (auto i = 0; i < 256; ++i)
390 {
391 auto const goodSize = negUnlSizeTest(l, 1, true, false);
392 BEAST_EXPECT(goodSize);
393 if (goodSize)
394 {
395 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
396 BEAST_EXPECT(l->validatorToDisable() == publicKeys[0]);
397 }
399 }
400 BEAST_EXPECT(l->isFlagLedger());
401 l->updateNegativeUNL();
402
403 //(8) next flag ledger
404 // test if the ledger updated correctly
405 auto goodSize = negUnlSizeTest(l, 2, false, false);
406 BEAST_EXPECT(goodSize);
407 if (goodSize)
408 {
409 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
410 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
411 nUnlLedgerSeq.emplace(publicKeys[0], l->seq());
412 BEAST_EXPECT(verifyPubKeyAndSeq(l, nUnlLedgerSeq));
413 }
414
415 auto const txDisable0 = createTx(true, l->seq(), publicKeys[0]);
416 auto const txReEnable0 = createTx(false, l->seq(), publicKeys[0]);
417 auto const txReEnable1 = createTx(false, l->seq(), publicKeys[1]);
418
419 OpenView accum(&*l);
420 BEAST_EXPECT(applyAndTestResult(env, accum, txReEnable0, true));
421 BEAST_EXPECT(applyAndTestResult(env, accum, txReEnable1, false));
422 BEAST_EXPECT(applyAndTestResult(env, accum, txDisable0, false));
423 accum.apply(*l);
424 goodSize = negUnlSizeTest(l, 2, false, true);
425 BEAST_EXPECT(goodSize);
426 if (goodSize)
427 {
428 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
429 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
430 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[0]);
431 BEAST_EXPECT(verifyPubKeyAndSeq(l, nUnlLedgerSeq));
432 }
433 }
434
435 {
436 //(9) ledgers before the next flag ledger
437 for (auto i = 0; i < 256; ++i)
438 {
439 auto const goodSize = negUnlSizeTest(l, 2, false, true);
440 BEAST_EXPECT(goodSize);
441 if (goodSize)
442 {
443 BEAST_EXPECT(l->negativeUNL().count(publicKeys[0]));
444 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
445 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[0]);
446 }
448 }
449 BEAST_EXPECT(l->isFlagLedger());
450 l->updateNegativeUNL();
451
452 //(10) next flag ledger
453 // test if the ledger updated correctly
454 auto goodSize = negUnlSizeTest(l, 1, false, false);
455 BEAST_EXPECT(goodSize);
456 if (goodSize)
457 {
458 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
459 nUnlLedgerSeq.erase(publicKeys[0]);
460 BEAST_EXPECT(verifyPubKeyAndSeq(l, nUnlLedgerSeq));
461 }
462
463 auto const txReEnable1 = createTx(false, l->seq(), publicKeys[1]);
464
465 OpenView accum(&*l);
466 BEAST_EXPECT(applyAndTestResult(env, accum, txReEnable1, true));
467 accum.apply(*l);
468 goodSize = negUnlSizeTest(l, 1, false, true);
469 BEAST_EXPECT(goodSize);
470 if (goodSize)
471 {
472 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
473 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[1]);
474 BEAST_EXPECT(verifyPubKeyAndSeq(l, nUnlLedgerSeq));
475 }
476 }
477
478 {
479 //(11) ledgers before the next flag ledger
480 for (auto i = 0; i < 256; ++i)
481 {
482 auto const goodSize = negUnlSizeTest(l, 1, false, true);
483 BEAST_EXPECT(goodSize);
484 if (goodSize)
485 {
486 BEAST_EXPECT(l->negativeUNL().count(publicKeys[1]));
487 BEAST_EXPECT(l->validatorToReEnable() == publicKeys[1]);
488 }
490 }
491 BEAST_EXPECT(l->isFlagLedger());
492 l->updateNegativeUNL();
493
494 //(12) next flag ledger
495 BEAST_EXPECT(negUnlSizeTest(l, 0, false, false));
496 }
497
498 {
499 //(13) ledgers before the next flag ledger
500 for (auto i = 0; i < 256; ++i)
501 {
502 BEAST_EXPECT(negUnlSizeTest(l, 0, false, false));
504 }
505 BEAST_EXPECT(l->isFlagLedger());
506 l->updateNegativeUNL();
507
508 //(14) next flag ledger
509 BEAST_EXPECT(negUnlSizeTest(l, 0, false, false));
510 }
511 }
512
513 void
514 run() override
515 {
517 }
518};
519
524{
532 {
533 std::uint32_t numNodes; // number of validators
534 std::uint32_t negUNLSize; // size of negative UNL in the last ledger
535 bool hasToDisable; // if has ToDisable in the last ledger
536 bool hasToReEnable; // if has ToReEnable in the last ledger
542 };
543
545 : env(suite, jtx::testableAmendments()), param(p), validations(env.app().getValidations())
546 {
547 createNodes();
548 if (!param.numLedgers)
549 param.numLedgers = 256 * (param.negUNLSize + 1);
551 }
552
553 void
555 {
556 assert(param.numNodes <= 256);
557 unlKeys = createPublicKeys(param.numNodes);
558 for (int i = 0; i < param.numNodes; ++i)
559 {
560 unlKeySet.insert(unlKeys[i]);
561 unlNodeIDs.push_back(calcNodeID(unlKeys[i]));
562 unlNodeIdSet.insert(unlNodeIDs.back());
563 }
564 }
565
570 bool
572 {
573 static UInt256 kFakeAmendment; // So we have different genesis ledgers
576 Rules{env.app().config().features},
577 env.app().config().fees.toFees(),
578 std::vector<UInt256>{kFakeAmendment++},
579 env.app().getNodeFamily());
580 history.push_back(l);
581
582 // When putting validators into the negative UNL, we start with
583 // validator 0, then validator 1 ...
584 int nidx = 0;
585 while (l->seq() <= param.numLedgers)
586 {
587 l = std::make_shared<Ledger>(*l, env.app().getTimeKeeper().closeTime());
588 history.push_back(l);
589
590 if (l->isFlagLedger())
591 {
592 l->updateNegativeUNL();
593 OpenView accum(&*l);
594 if (l->negativeUNL().size() < param.negUNLSize)
595 {
596 auto const tx = createTx(true, l->seq(), unlKeys[nidx]);
597 if (!applyAndTestResult(env, accum, tx, true))
598 break;
599 ++nidx;
600 }
601 else if (l->negativeUNL().size() == param.negUNLSize)
602 {
603 if (param.hasToDisable)
604 {
605 auto const tx = createTx(true, l->seq(), unlKeys[nidx]);
606 if (!applyAndTestResult(env, accum, tx, true))
607 break;
608 ++nidx;
609 }
610 if (param.hasToReEnable)
611 {
612 auto const tx = createTx(false, l->seq(), unlKeys[0]);
613 if (!applyAndTestResult(env, accum, tx, true))
614 break;
615 }
616 }
617 accum.apply(*l);
618 }
619 l->updateSkipList();
620 }
621 return negUnlSizeTest(l, param.negUNLSize, param.hasToDisable, param.hasToReEnable);
622 }
623
632 {
633 static auto const kEyPair = randomKeyPair(KeyType::Secp256k1);
635 env.app().getTimeKeeper().now(),
636 kEyPair.first,
637 kEyPair.second,
638 v,
639 [&](STValidation& v) {
640 v.setFieldH256(sfLedgerHash, ledger->header().hash);
641 v.setFieldU32(sfLedgerSequence, ledger->seq());
642 v.setFlag(kVfFullValidation);
643 });
644 };
645
653 template <class NeedValidation>
654 void
655 walkHistoryAndAddValidations(NeedValidation&& needVal)
656 {
657 std::uint32_t curr = 0;
658 std::size_t const need = 256 + 1;
659 // only last 256 + 1 ledgers need validations
660 if (history.size() > need)
661 curr = history.size() - need;
662 for (; curr != history.size(); ++curr)
663 {
664 for (std::size_t i = 0; i < param.numNodes; ++i)
665 {
666 if (needVal(history[curr], i))
667 {
669 v.setTrusted();
670 validations.add(unlNodeIDs[i], v);
671 }
672 }
673 }
674 }
675
678 {
679 return history.back();
680 }
681
691};
692
705template <typename PreVote = decltype(gDefaultPreVote)>
706bool
708 NetworkHistory& history,
709 NodeID const& myId,
710 std::size_t expect,
711 PreVote const& pre = gDefaultPreVote)
712{
713 NegativeUNLVote vote(myId, history.env.journal);
714 pre(vote);
715 auto const txSet =
717 vote.doVoting(history.lastLedger(), history.unlKeySet, history.validations, txSet);
718 return countTx(txSet) == expect;
719}
720
725{
726 void
728 {
729 testcase("Create UNLModify Tx");
730 jtx::Env env(*this);
731
732 NodeID const myId(0xA0);
733 NegativeUNLVote vote(myId, env.journal);
734
735 // one add, one remove
736 auto const txSet =
740 LedgerIndex const seq(1234);
741 BEAST_EXPECT(countTx(txSet) == 0);
742 vote.addTx(seq, toDisableKey, NegativeUNLVote::NegativeUNLModify::ToDisable, txSet);
743 BEAST_EXPECT(countTx(txSet) == 1);
744 vote.addTx(seq, toReEnableKey, NegativeUNLVote::NegativeUNLModify::ToReEnable, txSet);
745 BEAST_EXPECT(countTx(txSet) == 2);
746 // content of a tx is implicitly tested after applied to a ledger
747 // in later test cases
748 }
749
750 void
752 {
753 testcase("Pick One Candidate");
754 jtx::Env const env(*this);
755
756 NodeID const myId(0xA0);
757 NegativeUNLVote const vote(myId, env.journal);
758
759 UInt256 const pad0(0);
760 UInt256 const padF = ~pad0;
761 NodeID const n1(1);
762 NodeID const n2(2);
763 NodeID const n3(3);
764 std::vector<NodeID> candidates({n1});
765 BEAST_EXPECT(vote.choose(pad0, candidates) == n1);
766 BEAST_EXPECT(vote.choose(padF, candidates) == n1);
767 candidates.emplace_back(2);
768 BEAST_EXPECT(vote.choose(pad0, candidates) == n1);
769 BEAST_EXPECT(vote.choose(padF, candidates) == n2);
770 candidates.emplace_back(3);
771 BEAST_EXPECT(vote.choose(pad0, candidates) == n1);
772 BEAST_EXPECT(vote.choose(padF, candidates) == n3);
773 }
774
775 void
777 {
778 testcase("Build Score Table");
779 /*
780 * 1. no skip list
781 * 2. short skip list
782 * 3. local node not enough history
783 * 4. a node double validated some seq
784 * 5. local node had enough validations but on a wrong chain
785 * 6. a good case, long enough history and perfect scores
786 */
787 {
788 // 1. no skip list
789 NetworkHistory history = {
790 *this,
791 {
792 .numNodes = 10,
793 .negUNLSize = 0,
794 .hasToDisable = false,
795 .hasToReEnable = false,
796 .numLedgers = 1,
797 }};
798 BEAST_EXPECT(history.goodHistory);
799 if (history.goodHistory)
800 {
801 NegativeUNLVote vote(history.unlNodeIDs[3], history.env.journal);
802 BEAST_EXPECT(!vote.buildScoreTable(
803 history.lastLedger(), history.unlNodeIdSet, history.validations));
804 }
805 }
806
807 {
808 // 2. short skip list
809 NetworkHistory history = {
810 *this,
811 {
812 .numNodes = 10,
813 .negUNLSize = 0,
814 .hasToDisable = false,
815 .hasToReEnable = false,
816 .numLedgers = 256 / 2,
817 }};
818 BEAST_EXPECT(history.goodHistory);
819 if (history.goodHistory)
820 {
821 NegativeUNLVote vote(history.unlNodeIDs[3], history.env.journal);
822 BEAST_EXPECT(!vote.buildScoreTable(
823 history.lastLedger(), history.unlNodeIdSet, history.validations));
824 }
825 }
826
827 {
828 // 3. local node not enough history
829 NetworkHistory history = {
830 *this,
831 {
832 .numNodes = 10,
833 .negUNLSize = 0,
834 .hasToDisable = false,
835 .hasToReEnable = false,
836 .numLedgers = 256 + 2,
837 }};
838 BEAST_EXPECT(history.goodHistory);
839 if (history.goodHistory)
840 {
841 NodeID myId = history.unlNodeIDs[3];
843 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
844 // skip half my validations.
845 return history.unlNodeIDs[idx] != myId || l->seq() % 2 != 0;
846 });
847 NegativeUNLVote vote(myId, history.env.journal);
848 BEAST_EXPECT(!vote.buildScoreTable(
849 history.lastLedger(), history.unlNodeIdSet, history.validations));
850 }
851 }
852
853 {
854 // 4. a node double validated some seq
855 // 5. local node had enough validations but on a wrong chain
856 NetworkHistory history = {
857 *this,
858 {
859 .numNodes = 10,
860 .negUNLSize = 0,
861 .hasToDisable = false,
862 .hasToReEnable = false,
863 .numLedgers = 256 + 2,
864 }};
865 // We need two chains for these tests
866 bool const wrongChainSuccess = history.goodHistory;
867 BEAST_EXPECT(wrongChainSuccess);
868 NetworkHistory::LedgerHistory wrongChain = std::move(history.history);
869 // Create a new chain and use it as the one that majority of nodes
870 // follow
871 history.createLedgerHistory();
872 BEAST_EXPECT(history.goodHistory);
873
874 if (history.goodHistory && wrongChainSuccess)
875 {
876 NodeID myId = history.unlNodeIDs[3];
877 NodeID const badNode = history.unlNodeIDs[4];
879 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
880 // everyone but me
881 return !(history.unlNodeIDs[idx] == myId);
882 });
883
884 // local node validate wrong chain
885 // a node double validates
886 for (auto const& l : wrongChain)
887 {
888 RCLValidation const v1(history.createSTVal(l, myId));
889 history.validations.add(myId, v1);
890 RCLValidation const v2(history.createSTVal(l, badNode));
891 history.validations.add(badNode, v2);
892 }
893
894 NegativeUNLVote vote(myId, history.env.journal);
895
896 // local node still on wrong chain, can build a scoreTable,
897 // but all other nodes' scores are zero
898 auto scoreTable = vote.buildScoreTable(
899 wrongChain.back(), history.unlNodeIdSet, history.validations);
900 BEAST_EXPECT(scoreTable);
901 if (scoreTable)
902 {
903 for (auto const& [n, score] : *scoreTable)
904 {
905 if (n == myId)
906 {
907 BEAST_EXPECT(score == 256);
908 }
909 else
910 {
911 BEAST_EXPECT(score == 0);
912 }
913 }
914 }
915
916 // if local node switched to right history, but cannot build
917 // scoreTable because not enough local validations
918 BEAST_EXPECT(!vote.buildScoreTable(
919 history.lastLedger(), history.unlNodeIdSet, history.validations));
920 }
921 }
922
923 {
924 // 6. a good case
925 NetworkHistory history = {
926 *this,
927 {
928 .numNodes = 10,
929 .negUNLSize = 0,
930 .hasToDisable = false,
931 .hasToReEnable = false,
932 .numLedgers = 256 + 1,
933 }};
934 BEAST_EXPECT(history.goodHistory);
935 if (history.goodHistory)
936 {
938 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
939 return true;
940 });
941 NegativeUNLVote vote(history.unlNodeIDs[3], history.env.journal);
942 auto scoreTable = vote.buildScoreTable(
943 history.lastLedger(), history.unlNodeIdSet, history.validations);
944 BEAST_EXPECT(scoreTable);
945 if (scoreTable)
946 {
947 for (auto const& [_, score] : *scoreTable)
948 {
949 (void)_;
950 BEAST_EXPECT(score == 256);
951 }
952 }
953 }
954 }
955 }
956
969 static bool
971 NegativeUNLVote& vote,
972 HashSet<NodeID> const& unl,
973 HashSet<NodeID> const& negUnl,
974 HashMap<NodeID, std::uint32_t> const& scoreTable,
975 std::size_t numDisable,
976 std::size_t numReEnable)
977 {
978 auto [disableCandidates, reEnableCandidates] =
979 vote.findAllCandidates(unl, negUnl, scoreTable);
980 bool const rightDisable = disableCandidates.size() == numDisable;
981 bool const rightReEnable = reEnableCandidates.size() == numReEnable;
982 return rightDisable && rightReEnable;
983 };
984
985 void
987 {
988 testcase("Find All Candidates");
989 /*
990 * -- unl size: 35
991 * -- negUnl size: 3
992 *
993 * 0. all good scores
994 * 1. all bad scores
995 * 2. all between watermarks
996 * 3. 2 good scorers in negUnl
997 * 4. 2 bad scorers not in negUnl
998 * 5. 2 in negUnl but not in unl, have a remove candidate from score
999 * table
1000 * 6. 2 in negUnl but not in unl, no remove candidate from score table
1001 * 7. 2 new validators have good scores, already in negUnl
1002 * 8. 2 new validators have bad scores, not in negUnl
1003 * 9. expired the new validators have bad scores, not in negUnl
1004 */
1005 NetworkHistory history = {
1006 *this,
1007 {
1008 .numNodes = 35,
1009 .negUNLSize = 0,
1010 .hasToDisable = false,
1011 .hasToReEnable = false,
1012 .numLedgers = 0,
1013 }};
1014
1015 HashSet<NodeID> negUnl012;
1016 for (std::uint32_t i = 0; i < 3; ++i)
1017 negUnl012.insert(history.unlNodeIDs[i]);
1018
1019 // build a good scoreTable to use, or copy and modify
1020 HashMap<NodeID, std::uint32_t> goodScoreTable;
1021 for (auto const& n : history.unlNodeIDs)
1022 goodScoreTable[n] = NegativeUNLVote::kNegativeUnlHighWaterMark + 1;
1023
1024 NegativeUNLVote vote(history.unlNodeIDs[0], history.env.journal);
1025
1026 {
1027 // all good scores
1028 BEAST_EXPECT(
1029 checkCandidateSizes(vote, history.unlNodeIdSet, negUnl012, goodScoreTable, 0, 3));
1030 }
1031 {
1032 // all bad scores
1034 for (auto const& n : history.unlNodeIDs)
1036 BEAST_EXPECT(
1037 checkCandidateSizes(vote, history.unlNodeIdSet, negUnl012, scoreTable, 35 - 3, 0));
1038 }
1039 {
1040 // all between watermarks
1042 for (auto const& n : history.unlNodeIDs)
1044 BEAST_EXPECT(
1045 checkCandidateSizes(vote, history.unlNodeIdSet, negUnl012, scoreTable, 0, 0));
1046 }
1047
1048 {
1049 // 2 good scorers in negUnl
1050 auto scoreTable = goodScoreTable;
1051 scoreTable[*negUnl012.begin()] = NegativeUNLVote::kNegativeUnlLowWaterMark + 1;
1052 BEAST_EXPECT(
1053 checkCandidateSizes(vote, history.unlNodeIdSet, negUnl012, scoreTable, 0, 2));
1054 }
1055
1056 {
1057 // 2 bad scorers not in negUnl
1058 auto scoreTable = goodScoreTable;
1059 scoreTable[history.unlNodeIDs[11]] = NegativeUNLVote::kNegativeUnlLowWaterMark - 1;
1060 scoreTable[history.unlNodeIDs[12]] = NegativeUNLVote::kNegativeUnlLowWaterMark - 1;
1061 BEAST_EXPECT(
1062 checkCandidateSizes(vote, history.unlNodeIdSet, negUnl012, scoreTable, 2, 3));
1063 }
1064
1065 {
1066 // 2 in negUnl but not in unl, have a remove candidate from score
1067 // table
1068 HashSet<NodeID> unlTemp = history.unlNodeIdSet;
1069 unlTemp.erase(history.unlNodeIDs[0]);
1070 unlTemp.erase(history.unlNodeIDs[1]);
1071 BEAST_EXPECT(checkCandidateSizes(vote, unlTemp, negUnl012, goodScoreTable, 0, 3));
1072 }
1073
1074 {
1075 // 2 in negUnl but not in unl, no remove candidate from score table
1076 auto scoreTable = goodScoreTable;
1077 scoreTable.erase(history.unlNodeIDs[0]);
1078 scoreTable.erase(history.unlNodeIDs[1]);
1079 scoreTable[history.unlNodeIDs[2]] = NegativeUNLVote::kNegativeUnlLowWaterMark + 1;
1080 HashSet<NodeID> unlTemp = history.unlNodeIdSet;
1081 unlTemp.erase(history.unlNodeIDs[0]);
1082 unlTemp.erase(history.unlNodeIDs[1]);
1083 BEAST_EXPECT(checkCandidateSizes(vote, unlTemp, negUnl012, scoreTable, 0, 2));
1084 }
1085
1086 {
1087 // 2 new validators
1088 NodeID const new1(0xbead);
1089 NodeID const new2(0xbeef);
1090 HashSet<NodeID> const nowTrusted = {new1, new2};
1091 HashSet<NodeID> unlTemp = history.unlNodeIdSet;
1092 unlTemp.insert(new1);
1093 unlTemp.insert(new2);
1094 vote.newValidators(256, nowTrusted);
1095 {
1096 // 2 new validators have good scores, already in negUnl
1097 auto scoreTable = goodScoreTable;
1098 scoreTable[new1] = NegativeUNLVote::kNegativeUnlHighWaterMark + 1;
1099 scoreTable[new2] = NegativeUNLVote::kNegativeUnlHighWaterMark + 1;
1100 HashSet<NodeID> negUnlTemp = negUnl012;
1101 negUnlTemp.insert(new1);
1102 negUnlTemp.insert(new2);
1103 BEAST_EXPECT(checkCandidateSizes(vote, unlTemp, negUnlTemp, scoreTable, 0, 3 + 2));
1104 }
1105 {
1106 // 2 new validators have bad scores, not in negUnl
1107 auto scoreTable = goodScoreTable;
1108 scoreTable[new1] = 0;
1109 scoreTable[new2] = 0;
1110 BEAST_EXPECT(checkCandidateSizes(vote, unlTemp, negUnl012, scoreTable, 0, 3));
1111 }
1112 {
1113 // expired the new validators have bad scores, not in negUnl
1115 auto scoreTable = goodScoreTable;
1116 scoreTable[new1] = 0;
1117 scoreTable[new2] = 0;
1118 BEAST_EXPECT(checkCandidateSizes(vote, unlTemp, negUnl012, scoreTable, 2, 3));
1119 }
1120 }
1121 }
1122
1123 void
1125 {
1126 testcase("Find All Candidates Combination");
1127 /*
1128 * == combination 1:
1129 * -- unl size: 34, 35, 80
1130 * -- nUnl size: 0, 50%, all
1131 * -- score pattern: all 0, all negativeUNLLowWaterMark & +1 & -1, all
1132 * negativeUNLHighWaterMark & +1 & -1, all 100%
1133 *
1134 * == combination 2:
1135 * -- unl size: 34, 35, 80
1136 * -- negativeUNL size: 0, all
1137 * -- nUnl size: one on, one off, one on, one off,
1138 * -- score pattern: 2*(negativeUNLLowWaterMark, +1, -1) &
1139 * 2*(negativeUNLHighWaterMark, +1, -1) & rest
1140 * negativeUNLMinLocalValsToVote
1141 */
1142
1143 jtx::Env const env(*this);
1144
1145 NodeID const myId(0xA0);
1146 NegativeUNLVote vote(myId, env.journal);
1147
1148 std::array<std::uint32_t, 3> const unlSizes = {34, 35, 80};
1149 std::array<std::uint32_t, 3> const nUnlPercent = {0, 50, 100};
1151 0,
1159 };
1160
1161 //== combination 1:
1162 {
1163 auto const fillScoreTable = [&](std::uint32_t unlSize,
1164 std::uint32_t nUnlSize,
1165 std::uint32_t score,
1166 HashSet<NodeID>& unl,
1167 HashSet<NodeID>& negUnl,
1168 HashMap<NodeID, std::uint32_t>& scoreTable) {
1169 std::vector<NodeID> nodeIDs;
1170 std::vector<PublicKey> const keys = createPublicKeys(unlSize);
1171 for (auto const& k : keys)
1172 {
1173 nodeIDs.emplace_back(calcNodeID(k));
1174 unl.emplace(nodeIDs.back());
1175 scoreTable[nodeIDs.back()] = score;
1176 }
1177 for (std::uint32_t i = 0; i < nUnlSize; ++i)
1178 negUnl.insert(nodeIDs[i]);
1179 };
1180
1181 for (auto const us : unlSizes)
1182 {
1183 for (auto const np : nUnlPercent)
1184 {
1185 for (auto const score : scores)
1186 {
1187 HashSet<NodeID> unl;
1188 HashSet<NodeID> negUnl;
1190 fillScoreTable(us, us * np / 100, score, unl, negUnl, scoreTable);
1191 BEAST_EXPECT(unl.size() == us);
1192 BEAST_EXPECT(negUnl.size() == us * np / 100);
1193 BEAST_EXPECT(scoreTable.size() == us);
1194
1195 std::size_t toDisableExpect = 0;
1196 std::size_t toReEnableExpect = 0;
1197 if (np == 0)
1198 {
1200 {
1201 toDisableExpect = us;
1202 }
1203 }
1204 else if (np == 50)
1205 {
1207 {
1208 toReEnableExpect = us * np / 100;
1209 }
1210 }
1211 else
1212 {
1214 {
1215 toReEnableExpect = us;
1216 }
1217 }
1218 BEAST_EXPECT(checkCandidateSizes(
1219 vote, unl, negUnl, scoreTable, toDisableExpect, toReEnableExpect));
1220 }
1221 }
1222 }
1223
1224 //== combination 2:
1225 {
1226 auto const fillScoreTable = [&](std::uint32_t unlSize,
1227 std::uint32_t nUnlPercent,
1228 HashSet<NodeID>& unl,
1229 HashSet<NodeID>& negUnl,
1230 HashMap<NodeID, std::uint32_t>& scoreTable) {
1231 std::vector<NodeID> nodeIDs;
1232 std::vector<PublicKey> const keys = createPublicKeys(unlSize);
1233 for (auto const& k : keys)
1234 {
1235 nodeIDs.emplace_back(calcNodeID(k));
1236 unl.emplace(nodeIDs.back());
1237 }
1238
1239 std::uint32_t nIdx = 0;
1240 for (auto const score : scores)
1241 {
1242 scoreTable[nodeIDs[nIdx++]] = score;
1243 scoreTable[nodeIDs[nIdx++]] = score;
1244 }
1245 for (; nIdx < unlSize;)
1246 {
1247 scoreTable[nodeIDs[nIdx++]] = scores.back();
1248 }
1249
1250 if (nUnlPercent == 100)
1251 {
1252 negUnl = unl;
1253 }
1254 else if (nUnlPercent == 50)
1255 {
1256 for (std::uint32_t i = 1; i < unlSize; i += 2)
1257 negUnl.insert(nodeIDs[i]);
1258 }
1259 };
1260
1261 for (auto const us : unlSizes)
1262 {
1263 for (auto const np : nUnlPercent)
1264 {
1265 HashSet<NodeID> unl;
1266 HashSet<NodeID> negUnl;
1268
1269 fillScoreTable(us, np, unl, negUnl, scoreTable);
1270 BEAST_EXPECT(unl.size() == us);
1271 BEAST_EXPECT(negUnl.size() == us * np / 100);
1272 BEAST_EXPECT(scoreTable.size() == us);
1273
1274 std::size_t toDisableExpect = 0;
1275 std::size_t toReEnableExpect = 0;
1276 if (np == 0)
1277 {
1278 toDisableExpect = 4;
1279 }
1280 else if (np == 50)
1281 {
1282 toReEnableExpect = negUnl.size() - 6;
1283 }
1284 else
1285 {
1286 toReEnableExpect = negUnl.size() - 12;
1287 }
1288 BEAST_EXPECT(checkCandidateSizes(
1289 vote, unl, negUnl, scoreTable, toDisableExpect, toReEnableExpect));
1290 }
1291 }
1292 }
1293 }
1294 }
1295
1296 void
1298 {
1299 testcase("New Validators");
1300 jtx::Env const env(*this);
1301
1302 NodeID const myId(0xA0);
1303 NegativeUNLVote vote(myId, env.journal);
1304
1305 // test cases:
1306 // newValidators_ of the NegativeUNLVote empty, add one
1307 // add a new one and one already added
1308 // add a new one and some already added
1309 // purge and see some are expired
1310
1311 NodeID const n1(0xA1);
1312 NodeID const n2(0xA2);
1313 NodeID const n3(0xA3);
1314
1315 vote.newValidators(2, {n1});
1316 BEAST_EXPECT(vote.newValidators_.size() == 1);
1317 if (vote.newValidators_.size() == 1)
1318 {
1319 BEAST_EXPECT(vote.newValidators_.begin()->first == n1);
1320 BEAST_EXPECT(vote.newValidators_.begin()->second == 2);
1321 }
1322
1323 vote.newValidators(3, {n1, n2});
1324 BEAST_EXPECT(vote.newValidators_.size() == 2);
1325 if (vote.newValidators_.size() == 2)
1326 {
1327 BEAST_EXPECT(vote.newValidators_[n1] == 2);
1328 BEAST_EXPECT(vote.newValidators_[n2] == 3);
1329 }
1330
1332 BEAST_EXPECT(vote.newValidators_.size() == 3);
1333 if (vote.newValidators_.size() == 3)
1334 {
1335 BEAST_EXPECT(vote.newValidators_[n1] == 2);
1336 BEAST_EXPECT(vote.newValidators_[n2] == 3);
1338 }
1339
1341 BEAST_EXPECT(vote.newValidators_.size() == 3);
1343 BEAST_EXPECT(vote.newValidators_.size() == 2);
1345 BEAST_EXPECT(vote.newValidators_.size() == 1);
1346 BEAST_EXPECT(vote.newValidators_.begin()->first == n3);
1347 BEAST_EXPECT(
1349 }
1350
1351 void
1361};
1362
1369{
1370 void
1372 {
1373 testcase("Build Score Table Combination");
1374 /*
1375 * local node good history, correct scores:
1376 * == combination:
1377 * -- unl size: 10, 34, 35, 50
1378 * -- score pattern: all 0, all 50%, all 100%, two 0% two 50% rest 100%
1379 */
1380 std::array<std::uint32_t, 4> const unlSizes = {10, 34, 35, 50};
1381 std::array<std::array<std::uint32_t, 3>, 4> scorePattern = {
1382 {{{0, 0, 0}}, {{50, 50, 50}}, {{100, 100, 100}}, {{0, 50, 100}}},
1383 };
1384
1385 for (auto const unlSize : unlSizes)
1386 {
1387 for (std::uint32_t sp = 0; sp < 4; ++sp)
1388 {
1389 NetworkHistory history = {
1390 *this,
1391 {
1392 .numNodes = unlSize,
1393 .negUNLSize = 0,
1394 .hasToDisable = false,
1395 .hasToReEnable = false,
1396 .numLedgers = 256 + 2,
1397 }};
1398 BEAST_EXPECT(history.goodHistory);
1399 if (history.goodHistory)
1400 {
1401 NodeID myId = history.unlNodeIDs[3];
1403 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
1404 std::size_t k = 0;
1405 if (idx < 2)
1406 {
1407 k = 0;
1408 }
1409 else if (idx < 4)
1410 {
1411 k = 1;
1412 }
1413 else
1414 {
1415 k = 2;
1416 }
1417
1418 bool const add50 = scorePattern[sp][k] == 50 && l->seq() % 2 == 0;
1419 bool const add100 = scorePattern[sp][k] == 100;
1420 bool const addMe = history.unlNodeIDs[idx] == myId;
1421 return add50 || add100 || addMe;
1422 });
1423
1424 NegativeUNLVote vote(myId, history.env.journal);
1425 auto scoreTable = vote.buildScoreTable(
1426 history.lastLedger(), history.unlNodeIdSet, history.validations);
1427 BEAST_EXPECT(scoreTable);
1428 if (scoreTable)
1429 {
1430 std::uint32_t i = 0; // looping unl
1431 auto const checkScores = [&](std::uint32_t score, std::uint32_t k) -> bool {
1432 if (history.unlNodeIDs[i] == myId)
1433 return score == 256;
1434 if (scorePattern[sp][k] == 0)
1435 return score == 0;
1436 if (scorePattern[sp][k] == 50)
1437 return score == 256 / 2;
1438 if (scorePattern[sp][k] == 100)
1439 {
1440 return score == 256;
1441 }
1442
1443 return false;
1444 };
1445 for (; i < 2; ++i)
1446 {
1447 BEAST_EXPECT(checkScores((*scoreTable)[history.unlNodeIDs[i]], 0));
1448 }
1449 for (; i < 4; ++i)
1450 {
1451 BEAST_EXPECT(checkScores((*scoreTable)[history.unlNodeIDs[i]], 1));
1452 }
1453 for (; i < unlSize; ++i)
1454 {
1455 BEAST_EXPECT(checkScores((*scoreTable)[history.unlNodeIDs[i]], 2));
1456 }
1457 }
1458 }
1459 }
1460 }
1461 }
1462
1463 void
1464 run() override
1465 {
1467 }
1468};
1469
1470/*
1471 * Test the doVoting function of NegativeUNLVote.
1472 * The test cases are split to 5 classes for parallel execution.
1473 *
1474 * Voting tests: (use hasToDisable and hasToReEnable in some of the cases)
1475 *
1476 * == all good score, nUnl empty
1477 * -- txSet.size = 0
1478 * == all good score, nUnl not empty (use hasToDisable)
1479 * -- txSet.size = 1
1480 *
1481 * == 2 nodes offline, nUnl empty (use hasToReEnable)
1482 * -- txSet.size = 1
1483 * == 2 nodes offline, in nUnl
1484 * -- txSet.size = 0
1485 *
1486 * == 2 nodes offline, not in nUnl, but maxListed
1487 * -- txSet.size = 0
1488 *
1489 * == 2 nodes offline including me, not in nUnl
1490 * -- txSet.size = 0
1491 * == 2 nodes offline, not in negativeUNL, but I'm not a validator
1492 * -- txSet.size = 0
1493 * == 2 in nUnl, but not in unl, no other remove candidates
1494 * -- txSet.size = 1
1495 *
1496 * == 2 new validators have bad scores
1497 * -- txSet.size = 0
1498 * == 2 expired new validators have bad scores
1499 * -- txSet.size = 1
1500 */
1501
1503{
1504 void
1506 {
1507 testcase("Do Voting");
1508
1509 {
1510 //== all good score, negativeUNL empty
1511 //-- txSet.size = 0
1512 NetworkHistory history = {
1513 *this,
1514 {
1515 .numNodes = 51,
1516 .negUNLSize = 0,
1517 .hasToDisable = false,
1518 .hasToReEnable = false,
1519 .numLedgers = {},
1520 }};
1521 BEAST_EXPECT(history.goodHistory);
1522 if (history.goodHistory)
1523 {
1525 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
1526 return true;
1527 });
1528 BEAST_EXPECT(voteAndCheck(history, history.unlNodeIDs[0], 0));
1529 }
1530 }
1531
1532 {
1533 // all good score, negativeUNL not empty (use hasToDisable)
1534 //-- txSet.size = 1
1535 NetworkHistory history = {
1536 *this,
1537 {
1538 .numNodes = 37,
1539 .negUNLSize = 0,
1540 .hasToDisable = true,
1541 .hasToReEnable = false,
1542 .numLedgers = {},
1543 }};
1544 BEAST_EXPECT(history.goodHistory);
1545 if (history.goodHistory)
1546 {
1548 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
1549 return true;
1550 });
1551 BEAST_EXPECT(voteAndCheck(history, history.unlNodeIDs[0], 1));
1552 }
1553 }
1554 }
1555
1556 void
1557 run() override
1558 {
1559 testDoVoting();
1560 }
1561};
1562
1564{
1565 void
1567 {
1568 testcase("Do Voting");
1569
1570 {
1571 //== 2 nodes offline, negativeUNL empty (use hasToReEnable)
1572 //-- txSet.size = 1
1573 NetworkHistory history = {
1574 *this,
1575 {
1576 .numNodes = 29,
1577 .negUNLSize = 1,
1578 .hasToDisable = false,
1579 .hasToReEnable = true,
1580 .numLedgers = {},
1581 }};
1582 BEAST_EXPECT(history.goodHistory);
1583 if (history.goodHistory)
1584 {
1586 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
1587 // skip node 0 and node 1
1588 return idx > 1;
1589 });
1590 BEAST_EXPECT(voteAndCheck(history, history.unlNodeIDs.back(), 1));
1591 }
1592 }
1593
1594 {
1595 // 2 nodes offline, in negativeUNL
1596 //-- txSet.size = 0
1597 NetworkHistory history = {
1598 *this,
1599 {
1600 .numNodes = 30,
1601 .negUNLSize = 1,
1602 .hasToDisable = true,
1603 .hasToReEnable = false,
1604 .numLedgers = {},
1605 }};
1606 BEAST_EXPECT(history.goodHistory);
1607 if (history.goodHistory)
1608 {
1609 NodeID n1 = calcNodeID(*history.lastLedger()->negativeUNL().begin());
1610 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1611 NodeID n2 = calcNodeID(*history.lastLedger()->validatorToDisable());
1613 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
1614 // skip node 0 and node 1
1615 return history.unlNodeIDs[idx] != n1 && history.unlNodeIDs[idx] != n2;
1616 });
1617 BEAST_EXPECT(voteAndCheck(history, history.unlNodeIDs.back(), 0));
1618 }
1619 }
1620 }
1621
1622 void
1623 run() override
1624 {
1625 testDoVoting();
1626 }
1627};
1628
1630{
1631 void
1633 {
1634 testcase("Do Voting");
1635
1636 {
1637 // 2 nodes offline, not in negativeUNL, but maxListed
1638 //-- txSet.size = 0
1639 NetworkHistory history = {
1640 *this,
1641 {
1642 .numNodes = 32,
1643 .negUNLSize = 8,
1644 .hasToDisable = true,
1645 .hasToReEnable = true,
1646 .numLedgers = {},
1647 }};
1648 BEAST_EXPECT(history.goodHistory);
1649 if (history.goodHistory)
1650 {
1652 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
1653 // skip node 0 ~ 10
1654 return idx > 10;
1655 });
1656 BEAST_EXPECT(voteAndCheck(history, history.unlNodeIDs.back(), 0));
1657 }
1658 }
1659 }
1660
1661 void
1662 run() override
1663 {
1664 testDoVoting();
1665 }
1666};
1667
1669{
1670 void
1672 {
1673 testcase("Do Voting");
1674
1675 {
1676 //== 2 nodes offline including me, not in negativeUNL
1677 //-- txSet.size = 0
1678 NetworkHistory history = {
1679 *this,
1680 {
1681 .numNodes = 35,
1682 .negUNLSize = 0,
1683 .hasToDisable = false,
1684 .hasToReEnable = false,
1685 .numLedgers = {},
1686 }};
1687 BEAST_EXPECT(history.goodHistory);
1688 if (history.goodHistory)
1689 {
1691 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
1692 return idx > 1;
1693 });
1694 BEAST_EXPECT(voteAndCheck(history, history.unlNodeIDs[0], 0));
1695 }
1696 }
1697
1698 {
1699 // 2 nodes offline, not in negativeUNL, but I'm not a validator
1700 //-- txSet.size = 0
1701 NetworkHistory history = {
1702 *this,
1703 {
1704 .numNodes = 40,
1705 .negUNLSize = 0,
1706 .hasToDisable = false,
1707 .hasToReEnable = false,
1708 .numLedgers = {},
1709 }};
1710 BEAST_EXPECT(history.goodHistory);
1711 if (history.goodHistory)
1712 {
1714 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
1715 return idx > 1;
1716 });
1717 BEAST_EXPECT(voteAndCheck(history, NodeID(0xdeadbeef), 0));
1718 }
1719 }
1720
1721 {
1722 //== 2 in negativeUNL, but not in unl, no other remove candidates
1723 //-- txSet.size = 1
1724 NetworkHistory history = {
1725 *this,
1726 {
1727 .numNodes = 25,
1728 .negUNLSize = 2,
1729 .hasToDisable = false,
1730 .hasToReEnable = false,
1731 .numLedgers = {},
1732 }};
1733 BEAST_EXPECT(history.goodHistory);
1734 if (history.goodHistory)
1735 {
1737 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
1738 return idx > 1;
1739 });
1740 BEAST_EXPECT(
1741 voteAndCheck(history, history.unlNodeIDs.back(), 1, [&](NegativeUNLVote& vote) {
1742 history.unlKeySet.erase(history.unlKeys[0]);
1743 history.unlKeySet.erase(history.unlKeys[1]);
1744 }));
1745 }
1746 }
1747 }
1748
1749 void
1750 run() override
1751 {
1752 testDoVoting();
1753 }
1754};
1755
1757{
1758 void
1760 {
1761 testcase("Do Voting");
1762
1763 {
1764 //== 2 new validators have bad scores
1765 //-- txSet.size = 0
1766 NetworkHistory history = {
1767 *this,
1768 {
1769 .numNodes = 15,
1770 .negUNLSize = 0,
1771 .hasToDisable = false,
1772 .hasToReEnable = false,
1773 .numLedgers = {},
1774 }};
1775 BEAST_EXPECT(history.goodHistory);
1776 if (history.goodHistory)
1777 {
1779 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
1780 return true;
1781 });
1782 BEAST_EXPECT(
1783 voteAndCheck(history, history.unlNodeIDs[0], 0, [&](NegativeUNLVote& vote) {
1784 auto const extraKey1 = randomKeyPair(KeyType::Ed25519).first;
1785 auto const extraKey2 = randomKeyPair(KeyType::Ed25519).first;
1786 history.unlKeySet.insert(extraKey1);
1787 history.unlKeySet.insert(extraKey2);
1788 HashSet<NodeID> nowTrusted;
1789 nowTrusted.insert(calcNodeID(extraKey1));
1790 nowTrusted.insert(calcNodeID(extraKey2));
1791 vote.newValidators(history.lastLedger()->seq(), nowTrusted);
1792 }));
1793 }
1794 }
1795
1796 {
1797 //== 2 expired new validators have bad scores
1798 //-- txSet.size = 1
1799 NetworkHistory history = {
1800 *this,
1801 {
1802 .numNodes = 21,
1803 .negUNLSize = 0,
1804 .hasToDisable = false,
1805 .hasToReEnable = false,
1807 }};
1808 BEAST_EXPECT(history.goodHistory);
1809 if (history.goodHistory)
1810 {
1812 [&](std::shared_ptr<Ledger const> const& l, std::size_t idx) -> bool {
1813 return true;
1814 });
1815 BEAST_EXPECT(
1816 voteAndCheck(history, history.unlNodeIDs[0], 1, [&](NegativeUNLVote& vote) {
1817 auto const extraKey1 = randomKeyPair(KeyType::Ed25519).first;
1818 auto const extraKey2 = randomKeyPair(KeyType::Ed25519).first;
1819 history.unlKeySet.insert(extraKey1);
1820 history.unlKeySet.insert(extraKey2);
1821 HashSet<NodeID> nowTrusted;
1822 nowTrusted.insert(calcNodeID(extraKey1));
1823 nowTrusted.insert(calcNodeID(extraKey2));
1824 vote.newValidators(256, nowTrusted);
1825 }));
1826 }
1827 }
1828 }
1829
1830 void
1831 run() override
1832 {
1833 testDoVoting();
1834 }
1835};
1836
1838{
1839 void
1841 {
1842 testcase("Filter Validations");
1843 jtx::Env env(*this);
1844 auto l = std::make_shared<Ledger>(
1846 Rules{env.app().config().features},
1847 env.app().config().fees.toFees(),
1849 env.app().getNodeFamily());
1850
1851 auto const createSTVal = [&](std::pair<PublicKey, SecretKey> const& keys) {
1853 env.app().getTimeKeeper().now(),
1854 keys.first,
1855 keys.second,
1856 calcNodeID(keys.first),
1857 [&](STValidation& v) {
1858 v.setFieldH256(sfLedgerHash, l->header().hash);
1859 v.setFieldU32(sfLedgerSequence, l->seq());
1860 v.setFlag(kVfFullValidation);
1861 });
1862 };
1863
1864 // create keys and validations
1865 std::uint32_t const numNodes = 10;
1866 std::uint32_t const negUnlSize = 3;
1868 HashSet<NodeID> activeValidators;
1869 HashSet<PublicKey> nUnlKeys;
1871 for (int i = 0; i < numNodes; ++i)
1872 {
1873 auto const keyPair = randomKeyPair(KeyType::Secp256k1);
1874 vals.emplace_back(createSTVal(keyPair));
1875 cfgKeys.push_back(toBase58(TokenType::NodePublic, keyPair.first));
1876 activeValidators.emplace(calcNodeID(keyPair.first));
1877 if (i < negUnlSize)
1878 {
1879 nUnlKeys.insert(keyPair.first);
1880 }
1881 }
1882
1883 // setup the ValidatorList
1884 auto& validators = env.app().getValidators();
1885 auto& local = *nUnlKeys.begin();
1886 std::vector<std::string> const cfgPublishers;
1887 validators.load(local, cfgKeys, cfgPublishers);
1888 validators.updateTrusted(
1889 activeValidators,
1890 env.timeKeeper().now(),
1891 env.app().getOPs(),
1892 env.app().getOverlay(),
1893 env.app().getHashRouter());
1894 BEAST_EXPECT(validators.getTrustedMasterKeys().size() == numNodes);
1895 validators.setNegativeUNL(nUnlKeys);
1896 BEAST_EXPECT(validators.getNegativeUNL().size() == negUnlSize);
1897
1898 // test the filter
1899 BEAST_EXPECT(vals.size() == numNodes);
1900 vals = validators.negativeUNLFilter(std::move(vals));
1901 BEAST_EXPECT(vals.size() == numNodes - negUnlSize);
1902 }
1903
1904 void
1905 run() override
1906 {
1908 }
1909};
1910
1911BEAST_DEFINE_TESTSUITE(NegativeUNL, consensus, xrpl);
1912
1913BEAST_DEFINE_TESTSUITE(NegativeUNLVoteInternal, consensus, xrpl);
1914BEAST_DEFINE_TESTSUITE_MANUAL(NegativeUNLVoteScoreTable, consensus, xrpl);
1915BEAST_DEFINE_TESTSUITE_PRIO(NegativeUNLVoteGoodScore, consensus, xrpl, 1);
1916BEAST_DEFINE_TESTSUITE(NegativeUNLVoteOffline, consensus, xrpl);
1917BEAST_DEFINE_TESTSUITE(NegativeUNLVoteMaxListed, consensus, xrpl);
1918BEAST_DEFINE_TESTSUITE_PRIO(NegativeUNLVoteRetiredValidator, consensus, xrpl, 1);
1919BEAST_DEFINE_TESTSUITE(NegativeUNLVoteNewValidator, consensus, xrpl);
1920BEAST_DEFINE_TESTSUITE(NegativeUNLVoteFilterValidations, consensus, xrpl);
1921
1925bool
1928 size_t size,
1929 bool hasToDisable,
1930 bool hasToReEnable)
1931{
1932 bool const sameSize = l->negativeUNL().size() == size;
1933 bool const sameToDisable = (l->validatorToDisable() != std::nullopt) == hasToDisable;
1934 bool const sameToReEnable = (l->validatorToReEnable() != std::nullopt) == hasToReEnable;
1935
1936 return sameSize && sameToDisable && sameToReEnable;
1937}
1938
1939bool
1940applyAndTestResult(jtx::Env& env, OpenView& view, STTx const& tx, bool pass)
1941{
1942 auto const res = apply(env.app(), view, tx, ApplyFlags::TapNone, env.journal);
1943 if (pass)
1944 {
1945 return isTesSuccess(res.ter);
1946 }
1947
1948 return res.ter == tefFAILURE || res.ter == temDISABLED;
1949}
1950
1951bool
1955{
1956 auto const sle = l->read(keylet::negativeUNL());
1957 if (!sle)
1958 return false;
1959 if (!sle->isFieldPresent(sfDisabledValidators))
1960 return false;
1961
1962 auto const& nUnlData = sle->getFieldArray(sfDisabledValidators);
1963 if (nUnlData.size() != nUnlLedgerSeq.size())
1964 return false;
1965
1966 for (auto const& n : nUnlData)
1967 {
1968 if (!n.isFieldPresent(sfFirstLedgerSequence) || !n.isFieldPresent(sfPublicKey))
1969 return false;
1970
1971 auto seq = n.getFieldU32(sfFirstLedgerSequence);
1972 auto const d = n.getFieldVL(sfPublicKey);
1973 auto const s = makeSlice(d);
1974 if (!publicKeyType(s))
1975 return false;
1976 PublicKey const pk(s);
1977 auto const it = nUnlLedgerSeq.find(pk);
1978 if (it == nUnlLedgerSeq.end())
1979 return false;
1980 if (it->second != seq)
1981 return false;
1982 nUnlLedgerSeq.erase(it);
1983 }
1984 return nUnlLedgerSeq.empty();
1985}
1986
1989{
1990 std::size_t count = 0;
1991 for (auto i = txSet->begin(); i != txSet->end(); ++i)
1992 {
1993 ++count;
1994 }
1995 return count;
1996};
1997
2000{
2002 std::size_t const ss = 33;
2003 std::vector<uint8_t> data(ss, 0);
2004 data[0] = 0xED;
2005 for (int i = 0; i < n; ++i)
2006 {
2007 data[1]++;
2008 Slice const s(data.data(), ss);
2009 keys.emplace_back(s);
2010 }
2011 return keys;
2012}
2013
2014STTx
2015createTx(bool disabling, LedgerIndex seq, PublicKey const& txKey)
2016{
2017 auto const fill = [&](auto& obj) {
2018 obj.setFieldU8(sfUNLModifyDisabling, disabling ? 1 : 0);
2019 obj.setFieldU32(sfLedgerSequence, seq);
2020 obj.setFieldVL(sfUNLModifyValidator, txKey);
2021 };
2022 return STTx(ttUNL_MODIFY, fill);
2023}
2024
2025} // namespace xrpl::test
T back(T... args)
T begin(T... args)
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
virtual Config & config()=0
std::unordered_set< UInt256, beast::Uhash<> > features
Manager to create NegativeUNL votes.
static constexpr size_t kNegativeUnlHighWaterMark
An unreliable validator must have more than negativeUNLHighWaterMark validations in the last flag led...
static constexpr size_t kNegativeUnlMinLocalValsToVote
The minimum number of validations of the local node for it to participate in the voting.
std::optional< HashMap< NodeID, std::uint32_t > > buildScoreTable(std::shared_ptr< Ledger const > const &prevLedger, HashSet< NodeID > const &unl, RCLValidations &validations)
Build a reliability measurement score table of validators' validation messages in the last flag ledge...
static constexpr size_t kNewValidatorDisableSkip
We don't want to disable new validators immediately after adding them.
void purgeNewValidators(LedgerIndex seq)
Purge validators that are not new anymore.
Candidates findAllCandidates(HashSet< NodeID > const &unl, HashSet< NodeID > const &negUnl, HashMap< NodeID, std::uint32_t > const &scoreTable)
Process the score table and find all disabling and re-enabling candidates.
HashMap< NodeID, LedgerIndex > newValidators_
void doVoting(std::shared_ptr< Ledger const > const &prevLedger, HashSet< PublicKey > const &unlKeys, RCLValidations &validations, std::shared_ptr< SHAMap > const &initialSet)
Cast our local vote on the NegativeUNL candidates.
static constexpr size_t kNegativeUnlLowWaterMark
A validator is considered unreliable if its validations is less than negativeUNLLowWaterMark in the l...
void addTx(LedgerIndex seq, PublicKey const &vp, NegativeUNLModify modify, std::shared_ptr< SHAMap > const &initialSet)
Add a ttUNL_MODIFY Tx to the transaction set.
static NodeID choose(UInt256 const &randomPadData, std::vector< NodeID > const &candidates)
Pick one candidate from a vector of candidates.
void newValidators(LedgerIndex seq, HashSet< NodeID > const &nowTrusted)
Notify NegativeUNLVote that new validators are added.
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:59
void apply(TxsRawView &to) const
Apply changes.
Definition OpenView.cpp:129
A public key.
Definition PublicKey.h:53
Wrapper over STValidation for generic Validation code.
Rules controlling protocol behavior.
Definition Rules.h:40
virtual NetworkOPs & getOPs()=0
virtual ValidatorList & getValidators()=0
virtual HashRouter & getHashRouter()=0
virtual Family & getNodeFamily()=0
virtual Overlay & getOverlay()=0
virtual TimeKeeper & getTimeKeeper()=0
An immutable linear range of bytes.
Definition Slice.h:28
time_point now() const override
Returns the current time, using the server's clock.
Definition TimeKeeper.h:48
time_point closeTime() const
Returns the predicted close time, in network time.
Definition TimeKeeper.h:61
ValStatus add(NodeID const &nodeID, Validation const &val)
Add a new validation.
time_point now() const override
Returns the current time.
Test the private member functions of NegativeUNLVote.
static bool checkCandidateSizes(NegativeUNLVote &vote, HashSet< NodeID > const &unl, HashSet< NodeID > const &negUnl, HashMap< NodeID, std::uint32_t > const &scoreTable, std::size_t numDisable, std::size_t numReEnable)
Find all candidates and check if the number of candidates meets expectation.
Rest the build score table function of NegativeUNLVote.
void testNegativeUNL()
Test filling and applying ttUNL_MODIFY Tx, as well as ledger update:
void run() override
Runs the suite.
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
ManualTimeKeeper & timeKeeper()
Definition Env.h:313
beast::Journal const journal
Definition Env.h:204
T emplace_back(T... args)
T emplace(T... args)
T empty(T... args)
T end(T... args)
T erase(T... args)
T find(T... args)
T insert(T... args)
T make_shared(T... args)
Keylet const & negativeUNL() noexcept
The (fixed) index of the object containing the ledger negativeUNL.
Definition Indexes.cpp:274
FeatureBitset testableAmendments()
Definition Env.h:92
bool negUnlSizeTest(std::shared_ptr< Ledger const > const &l, size_t size, bool hasToDisable, bool hasToReEnable)
Test the size of the negative UNL in a ledger, also test if the ledger has ToDisable and/or ToReEnabl...
bool applyAndTestResult(jtx::Env &env, OpenView &view, STTx const &tx, bool pass)
Try to apply a ttUNL_MODIFY Tx, and test the apply result.
std::vector< PublicKey > createPublicKeys(std::size_t n)
Create fake public keys.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
bool verifyPubKeyAndSeq(std::shared_ptr< Ledger const > const &l, HashMap< PublicKey, std::uint32_t > nUnlLedgerSeq)
Verify the content of negative UNL entries (public key and ledger sequence) of a ledger.
bool voteAndCheck(NetworkHistory &history, NodeID const &myId, std::size_t expect, PreVote const &pre=gDefaultPreVote)
Create a NegativeUNLVote object.
BEAST_DEFINE_TESTSUITE_MANUAL(AMMCalc, app, xrpl)
STTx createTx(bool disabling, LedgerIndex seq, PublicKey const &txKey)
Create ttUNL_MODIFY Tx.
std::size_t countTx(std::shared_ptr< SHAMap > const &txSet)
Count the number of Tx in a TxSet.
BEAST_DEFINE_TESTSUITE_PRIO(AccountDelete, app, xrpl, 2)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
ApplyResult apply(ServiceRegistry &registry, OpenView &view, STTx const &tx, ApplyFlags flags, beast::Journal journal)
Apply a transaction to an OpenView.
Definition apply.cpp:181
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:409
CreateGenesisT const kCreateGenesis
@ tefFAILURE
Definition TER.h:160
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
BaseUInt< 256 > UInt256
Definition base_uint.h:581
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
Validations< RCLValidationsAdaptor > RCLValidations
Alias for RCL-specific instantiation of generic Validations.
std::unordered_set< Value, Hash, Pred, Allocator > HashSet
@ TapNone
Definition ApplyView.h:28
@ temDISABLED
Definition TER.h:102
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
std::unordered_map< Key, Value, Hash, Pred, Allocator > HashMap
BaseUInt< 160, detail::NodeIDTag > NodeID
NodeID is a 160-bit hash representing one node.
Definition UintTypes.h:47
T push_back(T... args)
T size(T... args)
Only reasonable parameters can be honored, e.g cannot hasToReEnable when nUNLSize == 0.
std::optional< int > numLedgers
if not specified, the number of ledgers in the history is calculated from negUNLSize,...
Utility class for creating validators and ledger history.
std::shared_ptr< STValidation > createSTVal(std::shared_ptr< Ledger const > const &ledger, NodeID const &v)
Create a validation.
void walkHistoryAndAddValidations(NeedValidation &&needVal)
Walk the ledger history and create validation messages for the ledgers.
std::shared_ptr< Ledger const > lastLedger() const
std::vector< PublicKey > unlKeys
bool createLedgerHistory()
create ledger history and apply needed ttUNL_MODIFY tx at flag ledgers
std::vector< std::shared_ptr< Ledger > > LedgerHistory
NetworkHistory(beast::unit_test::Suite &suite, Parameter const &p)
HashSet< PublicKey > unlKeySet
std::vector< NodeID > unlNodeIDs