xrpld
Loading...
Searching...
No Matches
RCLValidations_test.cpp
1
2#include <test/jtx/Env.h>
3
4#include <xrpld/app/consensus/RCLValidations.h>
5#include <xrpld/core/Config.h>
6
7#include <xrpl/basics/base_uint.h>
8#include <xrpl/basics/chrono.h>
9#include <xrpl/beast/unit_test/suite.h>
10#include <xrpl/consensus/LedgerTrie.h>
11#include <xrpl/ledger/Ledger.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/KeyType.h>
14#include <xrpl/protocol/PublicKey.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STValidation.h>
17#include <xrpl/protocol/SecretKey.h>
18
19#include <memory>
20#include <vector>
21
22namespace xrpl::test {
23
25{
26 void
28 {
29 testcase("Change validation trusted status");
30 auto const keys = randomKeyPair(KeyType::Secp256k1);
31 auto const v = std::make_shared<STValidation>(
33 keys.first,
34 keys.second,
35 calcNodeID(keys.first),
36 [&](STValidation& v) { v.setFieldU32(sfLedgerSequence, 123456); });
37
38 BEAST_EXPECT(v->isTrusted());
39 v->setUntrusted();
40 BEAST_EXPECT(!v->isTrusted());
41
42 RCLValidation rcv{v};
43 BEAST_EXPECT(!rcv.trusted());
44 rcv.setTrusted();
45 BEAST_EXPECT(rcv.trusted());
46 rcv.setUntrusted();
47 BEAST_EXPECT(!rcv.trusted());
48 }
49
50 void
52 {
53 testcase("RCLValidatedLedger ancestry");
54
56 using ID = RCLValidatedLedger::ID;
57
58 // This tests RCLValidatedLedger properly implements the type
59 // requirements of a LedgerTrie ledger, with its added behavior that
60 // only the 256 prior ledger hashes are available to determine ancestry.
61 Seq const maxAncestors = 256;
62
63 //----------------------------------------------------------------------
64 // Generate two ledger histories that agree on the first maxAncestors
65 // ledgers, then diverge.
66
68
69 jtx::Env env(*this);
70 Config const config;
73 Rules{config.features},
74 config.fees.toFees(),
76 env.app().getNodeFamily());
77 history.push_back(prev);
78 for (auto i = 0; i < ((2 * maxAncestors) + 1); ++i)
79 {
80 auto const next =
82 next->updateSkipList();
83 history.push_back(next);
84 prev = next;
85 }
86
87 // altHistory agrees with first half of regular history
88 Seq const diverge = history.size() / 2;
90 history.begin(), history.begin() + diverge);
91 // advance clock to get new ledgers
92 using namespace std::chrono_literals;
93 env.timeKeeper().set(env.timeKeeper().now() + 1200s);
94 prev = altHistory.back();
95 bool forceHash = true;
96 while (altHistory.size() < history.size())
97 {
98 auto const next =
100 // Force a different hash on the first iteration
101 next->updateSkipList();
102 BEAST_EXPECT(next->read(keylet::feeSettings()));
103 if (forceHash)
104 {
105 BEAST_EXPECT(next->setImmutable());
106 forceHash = false;
107 }
108
109 altHistory.push_back(next);
110 prev = next;
111 }
112
113 //----------------------------------------------------------------------
114
115 // Empty ledger
116 {
118 BEAST_EXPECT(a.seq() == Seq{0});
119 BEAST_EXPECT(a[Seq{0}] == ID{0});
120 BEAST_EXPECT(a.minSeq() == Seq{0});
121 }
122
123 // Full history ledgers
124 {
125 std::shared_ptr<Ledger const> const ledger = history.back();
126 RCLValidatedLedger const a{ledger, env.journal};
127 BEAST_EXPECT(a.seq() == ledger->header().seq);
128 BEAST_EXPECT(a.minSeq() == a.seq() - maxAncestors);
129 // Ensure the ancestral 256 ledgers have proper ID
130 for (Seq s = a.seq(); s > 0; s--)
131 {
132 if (s >= a.minSeq())
133 {
134 BEAST_EXPECT(a[s] == history[s - 1]->header().hash);
135 }
136 else
137 {
138 BEAST_EXPECT(a[s] == ID{0});
139 }
140 }
141 }
142
143 // Mismatch tests
144
145 // Empty with non-empty
146 {
148
149 for (auto const& ledger : {history.back(), history[maxAncestors - 1]})
150 {
151 RCLValidatedLedger const b{ledger, env.journal};
152 BEAST_EXPECT(mismatch(a, b) == 1);
153 BEAST_EXPECT(mismatch(b, a) == 1);
154 }
155 }
156 // Same chains, different seqs
157 {
158 RCLValidatedLedger const a{history.back(), env.journal};
159 for (Seq s = a.seq(); s > 0; s--)
160 {
161 RCLValidatedLedger const b{history[s - 1], env.journal};
162 if (s >= a.minSeq())
163 {
164 BEAST_EXPECT(mismatch(a, b) == b.seq() + 1);
165 BEAST_EXPECT(mismatch(b, a) == b.seq() + 1);
166 }
167 else
168 {
169 BEAST_EXPECT(mismatch(a, b) == Seq{1});
170 BEAST_EXPECT(mismatch(b, a) == Seq{1});
171 }
172 }
173 }
174 // Different chains, same seqs
175 {
176 // Alt history diverged at history.size()/2
177 for (Seq s = 1; s < history.size(); ++s)
178 {
179 RCLValidatedLedger const a{history[s - 1], env.journal};
180 RCLValidatedLedger const b{altHistory[s - 1], env.journal};
181
182 BEAST_EXPECT(a.seq() == b.seq());
183 if (s <= diverge)
184 {
185 BEAST_EXPECT(a[a.seq()] == b[b.seq()]);
186 BEAST_EXPECT(mismatch(a, b) == a.seq() + 1);
187 BEAST_EXPECT(mismatch(b, a) == a.seq() + 1);
188 }
189 else
190 {
191 BEAST_EXPECT(a[a.seq()] != b[b.seq()]);
192 BEAST_EXPECT(mismatch(a, b) == diverge + 1);
193 BEAST_EXPECT(mismatch(b, a) == diverge + 1);
194 }
195 }
196 }
197 // Different chains, different seqs
198 {
199 // Compare around the divergence point
200 RCLValidatedLedger const a{history[diverge], env.journal};
201 for (Seq offset = diverge / 2; offset < 3 * diverge / 2; ++offset)
202 {
203 RCLValidatedLedger const b{altHistory[offset - 1], env.journal};
204 if (offset <= diverge)
205 {
206 BEAST_EXPECT(mismatch(a, b) == b.seq() + 1);
207 }
208 else
209 {
210 BEAST_EXPECT(mismatch(a, b) == diverge + 1);
211 }
212 }
213 }
214 }
215
216 void
218 {
219 testcase("RCLValidatedLedger LedgerTrie");
220
221 // This test exposes an issue with the limited 256
222 // ancestor hash design of RCLValidatedLedger.
223 // There is only a single chain of validated ledgers
224 // but the 256 gap causes a "split" in the LedgerTrie
225 // due to the lack of ancestry information for a later ledger.
226 // This exposes a bug in which we are unable to remove
227 // support for a ledger hash which is already in the trie.
228
230
231 // Max known ancestors for each ledger
232 Seq const maxAncestors = 256;
234
235 // Generate a chain of 256 + 10 ledgers
236 jtx::Env env(*this);
237 auto& j = env.journal;
238 Config const config;
241 Rules{config.features},
242 config.fees.toFees(),
244 env.app().getNodeFamily());
245 history.push_back(prev);
246 for (auto i = 0; i < (maxAncestors + 10); ++i)
247 {
248 auto const next =
250 next->updateSkipList();
251 history.push_back(next);
252 prev = next;
253 }
254
256
257 // First, create the single branch trie, with ledgers
258 // separated by exactly 256 ledgers
259 auto const ledg002 = RCLValidatedLedger{history[1], j};
260 auto const ledg258 = RCLValidatedLedger{history[257], j};
261 auto const ledg259 = RCLValidatedLedger{history[258], j};
262
263 trie.insert(ledg002);
264 trie.insert(ledg258, 4);
265 // trie.dump(std::cout);
266 // 000000[0,1)(T:0,B:5)
267 // |-AB868A..36C8[1,3)(T:1,B:5)
268 // |-AB868A..37C8[3,259)(T:4,B:4)
269 BEAST_EXPECT(trie.tipSupport(ledg002) == 1);
270 BEAST_EXPECT(trie.branchSupport(ledg002) == 5);
271 BEAST_EXPECT(trie.tipSupport(ledg258) == 4);
272 BEAST_EXPECT(trie.branchSupport(ledg258) == 4);
273
274 // Move three of the s258 ledgers to s259, which splits the trie
275 // due to the 256 ancestry limit
276 BEAST_EXPECT(trie.remove(ledg258, 3));
277 trie.insert(ledg259, 3);
278 [[maybe_unused]] auto const unused1 = trie.getPreferred(1);
279 // trie.dump(std::cout);
280 // 000000[0,1)(T:0,B:5)
281 // |-AB868A..37C9[1,260)(T:3,B:3)
282 // |-AB868A..36C8[1,3)(T:1,B:2)
283 // |-AB868A..37C8[3,259)(T:1,B:1)
284 BEAST_EXPECT(trie.tipSupport(ledg002) == 1);
285 BEAST_EXPECT(trie.branchSupport(ledg002) == 2);
286 BEAST_EXPECT(trie.tipSupport(ledg258) == 1);
287 BEAST_EXPECT(trie.branchSupport(ledg258) == 1);
288 BEAST_EXPECT(trie.tipSupport(ledg259) == 3);
289 BEAST_EXPECT(trie.branchSupport(ledg259) == 3);
290
291 // The last call to trie.getPreferred cycled the children of the root
292 // node to make the new branch the first child (since it has support 3)
293 // then verify the remove call works
294 // past bug: remove had assumed the first child of a node in the trie
295 // which matches is the *only* child in the trie which matches.
296 // This is **NOT** true with the limited 256 ledger ancestry
297 // quirk of RCLValidation and prevents deleting the old support
298 // for ledger 257
299
300 BEAST_EXPECT(trie.remove(RCLValidatedLedger{history[257], env.journal}, 1));
301 trie.insert(RCLValidatedLedger{history[258], env.journal}, 1);
302 [[maybe_unused]] auto const unused2 = trie.getPreferred(1);
303 // trie.dump(std::cout);
304 // 000000[0,1)(T:0,B:5)
305 // |-AB868A..37C9[1,260)(T:4,B:4)
306 // |-AB868A..36C8[1,3)(T:1,B:1)
307 BEAST_EXPECT(trie.tipSupport(ledg002) == 1);
308 BEAST_EXPECT(trie.branchSupport(ledg002) == 1);
309 BEAST_EXPECT(trie.tipSupport(ledg258) == 0);
310 // 258 no longer lives on a tip in the tree, BUT it is an ancestor
311 // of 259 which is a tip and therefore gets it's branchSupport value
312 // implicitly
313 BEAST_EXPECT(trie.branchSupport(ledg258) == 4);
314 BEAST_EXPECT(trie.tipSupport(ledg259) == 4);
315 BEAST_EXPECT(trie.branchSupport(ledg259) == 4);
316 }
317
318public:
319 void
326};
327
329
330} // 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
std::unordered_set< UInt256, beast::Uhash<> > features
Ancestry trie of ledgers.
Definition LedgerTrie.h:331
std::uint32_t tipSupport(Ledger const &ledger) const
Return count of tip support for the specific ledger.
Definition LedgerTrie.h:582
std::uint32_t branchSupport(Ledger const &ledger) const
Return the count of branch support for the specific ledger.
Definition LedgerTrie.h:597
std::optional< SpanTip< Ledger > > getPreferred(Seq const largestIssued) const
Return the preferred ledger ID.
Definition LedgerTrie.h:672
void insert(Ledger const &ledger, std::uint32_t count=1)
Insert and/or increment the support for the given ledger.
Definition LedgerTrie.h:436
bool remove(Ledger const &ledger, std::uint32_t count=1)
Decrease support for a ledger, removing and compressing if possible.
Definition LedgerTrie.h:523
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
Wraps a ledger instance for use in generic Validations LedgerTrie.
Seq seq() const
The sequence (index) of the ledger.
Wrapper over STValidation for generic Validation code.
bool trusted() const
Whether the validation is considered trusted.
Rules controlling protocol behavior.
Definition Rules.h:40
virtual TimeKeeper & getTimeKeeper()=0
time_point closeTime() const
Returns the predicted close time, in network time.
Definition TimeKeeper.h:61
time_point now() const override
Returns the current time.
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 make_shared(T... args)
Keylet const & feeSettings() noexcept
The (fixed) index of the object containing the ledger fees.
Definition Indexes.cpp:267
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
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.
CreateGenesisT const kCreateGenesis
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
Validations< RCLValidationsAdaptor > RCLValidations
Alias for RCL-specific instantiation of generic Validations.
RCLValidatedLedger::Seq mismatch(RCLValidatedLedger const &a, RCLValidatedLedger const &b)
T push_back(T... args)
T size(T... args)
Fees toFees() const
Convert to a Fees object for use with Ledger construction.
Set the sequence number on a JTx.
Definition seq.h:16