xrpld
Loading...
Searching...
No Matches
LedgerHistory_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/CheckMessageLogs.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/JTx.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/envconfig.h>
7#include <test/jtx/noop.h>
8
9#include <xrpld/app/ledger/LedgerHistory.h>
10#include <xrpld/app/ledger/LedgerMaster.h>
11
12#include <xrpl/basics/base_uint.h>
13#include <xrpl/basics/chrono.h>
14#include <xrpl/basics/contract.h>
15#include <xrpl/beast/insight/NullCollector.h>
16#include <xrpl/beast/unit_test/suite.h>
17#include <xrpl/beast/utility/Journal.h>
18#include <xrpl/json/json_value.h>
19#include <xrpl/ledger/ApplyView.h>
20#include <xrpl/ledger/Ledger.h>
21#include <xrpl/ledger/OpenView.h>
22#include <xrpl/nodestore/NodeObject.h>
23#include <xrpl/protocol/STTx.h>
24#include <xrpl/tx/apply.h>
25
26#include <cassert>
27#include <memory>
28#include <stdexcept>
29#include <vector>
30
31namespace xrpl::test {
32
34{
35public:
46 jtx::Env& env,
47 LedgerHistory& lh,
48 NetClock::duration closeOffset,
50 {
51 if (!prev)
52 {
53 assert(!stx);
56 Rules{env.app().config().features},
57 env.app().config().fees.toFees(),
59 env.app().getNodeFamily());
60 }
61 auto res = std::make_shared<Ledger>(*prev, prev->header().closeTime + closeOffset);
62
63 if (stx)
64 {
65 OpenView accum(&*res);
66 applyTransaction(env.app(), accum, *stx, false, TapNone, env.journal);
67 accum.apply(*res);
68 }
69 res->updateSkipList();
70
71 {
72 res->stateMap().flushDirty(NodeObjectType::AccountNode);
73 res->txMap().flushDirty(NodeObjectType::TransactionNode);
74 }
75 res->unshare();
76
77 // Accept the ledger. Thrown rather than asserted because a refusal leaves res unusable,
78 // and this helper is static, so BEAST_EXPECT is out of reach.
79 if (!res->setAccepted(
80 res->header().closeTime,
81 res->header().closeTimeResolution,
82 true /* close time correct*/))
83 {
84 Throw<std::runtime_error>("makeLedger: ledger could not be accepted");
85 }
86 lh.insert(res, false);
87 return res;
88 }
89
90 void
92 {
93 testcase("LedgerHistory hash/index invariant");
94 using namespace jtx;
95 using namespace std::chrono;
96
97 Env env{*this};
99
100 // Create and insert validated ledgers
101 auto const genesis = makeLedger({}, env, lh, 0s);
102 auto const ledger1 = makeLedger(genesis, env, lh, 4s);
103 auto const ledger2 = makeLedger(ledger1, env, lh, 4s);
104 auto const ledger3 = makeLedger(ledger2, env, lh, 4s);
105
106 // Insert as validated (so they go into by_index)
107 lh.insert(genesis, true);
108 lh.insert(ledger1, true);
109 lh.insert(ledger2, true);
110 lh.insert(ledger3, true);
111
112 // Verify the hash/index invariant holds
113 // Can retrieve by sequence and get correct hash
114 BEAST_EXPECT(lh.getLedgerHash(genesis->header().seq) == genesis->header().hash);
115 BEAST_EXPECT(lh.getLedgerHash(ledger1->header().seq) == ledger1->header().hash);
116 BEAST_EXPECT(lh.getLedgerHash(ledger2->header().seq) == ledger2->header().hash);
117 BEAST_EXPECT(lh.getLedgerHash(ledger3->header().seq) == ledger3->header().hash);
118
119 // Can retrieve by sequence and get correct ledger
120 auto const fetched1 = lh.getLedgerBySeq(ledger1->header().seq);
121 if (BEAST_EXPECT(fetched1 != nullptr))
122 BEAST_EXPECT(fetched1->header().hash == ledger1->header().hash);
123
124 auto const fetched2 = lh.getLedgerBySeq(ledger2->header().seq);
125 if (BEAST_EXPECT(fetched2 != nullptr))
126 BEAST_EXPECT(fetched2->header().hash == ledger2->header().hash);
127
128 // Clear ledgers prior to ledger2's sequence
129 lh.clearLedgerCachePrior(ledger2->header().seq);
130
131 // Verify old entries are gone from the in-memory by_index map
132 // Note: getLedgerHash checks by_index directly without DB fallback
133 BEAST_EXPECT(lh.getLedgerHash(genesis->header().seq).isZero());
134 BEAST_EXPECT(lh.getLedgerHash(ledger1->header().seq).isZero());
135
136 // Verify newer entries are still present in by_index
137 BEAST_EXPECT(lh.getLedgerHash(ledger2->header().seq) == ledger2->header().hash);
138 BEAST_EXPECT(lh.getLedgerHash(ledger3->header().seq) == ledger3->header().hash);
139
140 // The by_hash cache must be pruned to the same cutoff. A cache hit
141 // hands back the very object that was inserted, so identity tells a
142 // hit from a miss. (A null check would not: ledger1 is a transaction-
143 // free successor of genesis and therefore has the same hash as the
144 // Env's own ledger 2, which getLedgerByHash can reload from SQL.)
145 BEAST_EXPECT(lh.getLedgerByHash(ledger2->header().hash).get() == ledger2.get());
146 BEAST_EXPECT(lh.getLedgerByHash(ledger3->header().hash).get() == ledger3.get());
147 BEAST_EXPECT(lh.getLedgerByHash(ledger1->header().hash).get() != ledger1.get());
148
149 // Verify newer entries remain retrievable and consistent
150 // getLedgerBySeq uses by_index first, then falls back to DB if needed
151 auto const fetched2After = lh.getLedgerBySeq(ledger2->header().seq);
152 if (BEAST_EXPECT(fetched2After != nullptr))
153 BEAST_EXPECT(fetched2After->header().hash == ledger2->header().hash);
154
155 auto const fetched3After = lh.getLedgerBySeq(ledger3->header().seq);
156 if (BEAST_EXPECT(fetched3After != nullptr))
157 BEAST_EXPECT(fetched3After->header().hash == ledger3->header().hash);
158 }
159
160 void
162 {
163 testcase("LedgerHistory mismatch");
164 using namespace jtx;
165 using namespace std::chrono;
166
167 // No mismatch
168 {
169 bool found = false;
170 Env env{*this, envconfig(), std::make_unique<CheckMessageLogs>("MISMATCH ", &found)};
172 auto const genesis = makeLedger({}, env, lh, 0s);
173 UInt256 const dummyTxHash{1};
174 lh.builtLedger(genesis, dummyTxHash, {});
175 lh.validatedLedger(genesis, dummyTxHash);
176
177 BEAST_EXPECT(!found);
178 }
179
180 // Close time mismatch
181 {
182 bool found = false;
183 Env env{
184 *this,
185 envconfig(),
186 std::make_unique<CheckMessageLogs>("MISMATCH on close time", &found)};
188 auto const genesis = makeLedger({}, env, lh, 0s);
189 auto const ledgerA = makeLedger(genesis, env, lh, 4s);
190 auto const ledgerB = makeLedger(genesis, env, lh, 40s);
191
192 UInt256 const dummyTxHash{1};
193 lh.builtLedger(ledgerA, dummyTxHash, {});
194 lh.validatedLedger(ledgerB, dummyTxHash);
195
196 BEAST_EXPECT(found);
197 }
198
199 // Prior ledger mismatch
200 {
201 bool found = false;
202 Env env{
203 *this,
204 envconfig(),
205 std::make_unique<CheckMessageLogs>("MISMATCH on prior ledger", &found)};
207 auto const genesis = makeLedger({}, env, lh, 0s);
208 auto const ledgerA = makeLedger(genesis, env, lh, 4s);
209 auto const ledgerB = makeLedger(genesis, env, lh, 40s);
210 auto const ledgerAC = makeLedger(ledgerA, env, lh, 4s);
211 auto const ledgerBD = makeLedger(ledgerB, env, lh, 4s);
212
213 UInt256 const dummyTxHash{1};
214 lh.builtLedger(ledgerAC, dummyTxHash, {});
215 lh.validatedLedger(ledgerBD, dummyTxHash);
216
217 BEAST_EXPECT(found);
218 }
219
220 // Simulate a bug in which consensus may agree on transactions, but
221 // somehow generate different ledgers
222 for (bool const txBug : {true, false})
223 {
224 std::string const msg = txBug ? "MISMATCH with same consensus transaction set"
225 : "MISMATCH on consensus transaction set";
226 bool found = false;
227 Env env{*this, envconfig(), std::make_unique<CheckMessageLogs>(msg, &found)};
229
230 Account const alice{"A1"};
231 Account const bob{"A2"};
232 env.fund(XRP(1000), alice, bob);
233 env.close();
234
235 auto const ledgerBase = env.app().getLedgerMaster().getClosedLedger();
236
237 JTx const txAlice = env.jt(noop(alice));
238 auto const ledgerA = makeLedger(ledgerBase, env, lh, 4s, txAlice.stx);
239
240 JTx const txBob = env.jt(noop(bob));
241 auto const ledgerB = makeLedger(ledgerBase, env, lh, 4s, txBob.stx);
242
243 lh.builtLedger(ledgerA, txAlice.stx->getTransactionID(), {});
244 // Simulate the bug by claiming ledgerB had the same consensus hash
245 // as ledgerA, but somehow generated different ledgers
247 ledgerB, txBug ? txAlice.stx->getTransactionID() : txBob.stx->getTransactionID());
248
249 BEAST_EXPECT(found);
250 }
251
252 // Reverse order: validatedLedger arrives first, then builtLedger
253 // detects the mismatch. Covers the mismatch branch in builtLedger.
254 {
255 bool found = false;
256 Env env{
257 *this,
258 envconfig(),
259 std::make_unique<CheckMessageLogs>("MISMATCH on close time", &found)};
261 auto const genesis = makeLedger({}, env, lh, 0s);
262 auto const ledgerA = makeLedger(genesis, env, lh, 4s);
263 auto const ledgerB = makeLedger(genesis, env, lh, 40s);
264
265 UInt256 const dummyTxHash{1};
266 lh.validatedLedger(ledgerB, dummyTxHash);
267 lh.builtLedger(ledgerA, dummyTxHash, {});
268
269 BEAST_EXPECT(found);
270 }
271
272 // The consensus JSON is captured inside the cache lock and handed to
273 // handleMismatch after it is released. The payload must survive that
274 // hand-off whichever side reports first: builtLedger passes its own
275 // argument, validatedLedger the copy stored in the cache entry.
276 for (bool const builtFirst : {true, false})
277 {
278 bool found = false;
279 // handleMismatch logs the consensus data at debug level, so raise
280 // the Env's log threshold (it defaults to Error) to capture it.
281 Env env{
282 *this,
283 envconfig(),
284 std::make_unique<CheckMessageLogs>("consensus-payload-marker", &found),
287 auto const genesis = makeLedger({}, env, lh, 0s);
288 auto const ledgerA = makeLedger(genesis, env, lh, 4s);
289 auto const ledgerB = makeLedger(genesis, env, lh, 40s);
290
292 consensus["marker"] = "consensus-payload-marker";
293
294 UInt256 const dummyTxHash{1};
295 if (builtFirst)
296 {
297 lh.builtLedger(ledgerA, dummyTxHash, consensus);
298 lh.validatedLedger(ledgerB, dummyTxHash);
299 }
300 else
301 {
302 lh.validatedLedger(ledgerB, dummyTxHash);
303 lh.builtLedger(ledgerA, dummyTxHash, consensus);
304 }
305
306 BEAST_EXPECT(found);
307 }
308 }
309
310 void
312 {
313 testcase("LedgerHistory fixIndex");
314 using namespace jtx;
315 using namespace std::chrono;
316
317 Env env{*this};
319
320 auto const genesis = makeLedger({}, env, lh, 0s);
321 auto const ledger1 = makeLedger(genesis, env, lh, 4s);
322 lh.insert(ledger1, true);
323
324 // Unknown index: returns true, no repair.
325 BEAST_EXPECT(lh.fixIndex(999, ledger1->header().hash));
326
327 // Known index with the same hash: returns true, no repair.
328 BEAST_EXPECT(lh.fixIndex(ledger1->header().seq, ledger1->header().hash));
329
330 // Known index with a different hash: returns false and repairs.
331 UInt256 const bogusHash{42};
332 BEAST_EXPECT(!lh.fixIndex(ledger1->header().seq, bogusHash));
333 BEAST_EXPECT(lh.getLedgerHash(ledger1->header().seq) == bogusHash);
334 }
335
336 void
337 run() override
338 {
341 testFixIndex();
342 }
343};
344
346
347} // namespace xrpl::test
static std::shared_ptr< Collector > make()
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
virtual Config & config()=0
bool isZero() const
Definition base_uint.h:563
std::unordered_set< UInt256, beast::Uhash<> > features
Retains historical ledgers.
bool insert(std::shared_ptr< Ledger const > const &ledger, bool validated)
Track a ledger.
LedgerHash getLedgerHash(LedgerIndex ledgerIndex)
Get a ledger's hash given its sequence number.
bool fixIndex(LedgerIndex ledgerIndex, LedgerHash const &ledgerHash)
Repair a hash to index mapping.
std::shared_ptr< Ledger const > getLedgerByHash(LedgerHash const &ledgerHash)
Retrieve a ledger given its hash.
void builtLedger(std::shared_ptr< Ledger const > const &, UInt256 const &consensusHash, json::Value)
Report that we have locally built a particular ledger.
std::shared_ptr< Ledger const > getLedgerBySeq(LedgerIndex ledgerIndex)
Get a ledger given its sequence number.
void validatedLedger(std::shared_ptr< Ledger const > const &, std::optional< UInt256 > const &consensusHash)
Report that we have validated a particular ledger.
void clearLedgerCachePrior(LedgerIndex seq)
std::shared_ptr< Ledger const > getClosedLedger()
The finalized ledger is the last closed/accepted ledger.
std::chrono::duration< rep, period > duration
Definition chrono.h:47
Rules controlling protocol behavior.
Definition Rules.h:40
virtual LedgerMaster & getLedgerMaster()=0
virtual Family & getNodeFamily()=0
static std::shared_ptr< Ledger > makeLedger(std::shared_ptr< Ledger const > const &prev, jtx::Env &env, LedgerHistory &lh, NetClock::duration closeOffset, std::shared_ptr< STTx const > stx={})
Generate a new ledger by hand, applying a specific close time offset and optionally inserting a trans...
void run() override
Runs the suite.
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
JTx jt(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition Env.h:723
beast::Journal const journal
Definition Env.h:204
T make_shared(T... args)
T make_unique(T... args)
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
json::Value noop(Account const &account)
The null transaction.
Definition noop.h:14
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:38
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
CreateGenesisT const kCreateGenesis
ApplyTransactionResult applyTransaction(ServiceRegistry &registry, OpenView &view, STTx const &tx, bool retryAssured, ApplyFlags flags, beast::Journal journal)
Transaction application helper.
Definition apply.cpp:267
BaseUInt< 256 > UInt256
Definition base_uint.h:581
@ TapNone
Definition ApplyView.h:28
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
Execution context for applying a JSON transaction.
Definition JTx.h:27
std::shared_ptr< STTx const > stx
Definition JTx.h:37