21#include <test/jtx/envconfig.h>
23#include <xrpld/app/main/Application.h>
24#include <xrpld/app/main/NodeStoreScheduler.h>
25#include <xrpld/app/misc/SHAMapStore.h>
26#include <xrpld/app/rdb/backend/SQLiteDatabase.h>
27#include <xrpld/core/ConfigSections.h>
28#include <xrpld/nodestore/detail/DatabaseRotatingImp.h>
30#include <xrpl/protocol/jss.h>
63 auto good =
json.isMember(jss::result) &&
65 json[jss::result][jss::ledger][jss::ledger_index] == ledgerID;
66 if (!good || !checkDB)
69 auto const seq =
json[jss::result][jss::ledger_index].asUInt();
91 auto const& ledger =
json[jss::result][jss::ledger];
92 return outHash == ledger[jss::ledger_hash].asString() &&
94 outParentHash == ledger[jss::parent_hash].asString() &&
95 outDrops == ledger[jss::total_coins].asString() &&
96 outCloseTime == ledger[jss::close_time].asUInt() &&
97 outParentCloseTime == ledger[jss::parent_close_time].asUInt() &&
98 outCloseTimeResolution ==
99 ledger[jss::close_time_resolution].asUInt() &&
100 outCloseFlags == ledger[jss::close_flags].asUInt() &&
101 outAccountHash == ledger[jss::account_hash].asString() &&
102 outTxHash == ledger[jss::transaction_hash].asString();
108 return json.isMember(jss::result) &&
110 json[jss::result][jss::error_code] == error;
117 json.isMember(jss::result) &&
118 json[jss::result].isMember(jss::ledger) &&
119 json[jss::result][jss::ledger].isMember(jss::ledger_hash) &&
120 json[jss::result][jss::ledger][jss::ledger_hash].isString());
121 return json[jss::result][jss::ledger][jss::ledger_hash].asString();
127 auto const [actualRows, actualFirst, actualLast] =
129 ->getLedgerCountMinMax();
131 BEAST_EXPECT(actualRows == rows);
132 BEAST_EXPECT(actualFirst == first);
133 BEAST_EXPECT(actualLast == first + rows - 1);
155 using namespace std::chrono_literals;
161 BEAST_EXPECT(!store.getLastRotated());
166 auto ledger = env.
rpc(
"ledger",
"validated");
169 BEAST_EXPECT(store.getLastRotated() == ledgerSeq - 1);
177 using namespace std::chrono_literals;
193 auto ledgerTmp = env.
rpc(
"ledger",
"0");
194 BEAST_EXPECT(
bad(ledgerTmp));
197 BEAST_EXPECT(
goodLedger(env, ledgers[1],
"1"));
200 BEAST_EXPECT(
goodLedger(env, ledgers[2],
"2"));
202 ledgerTmp = env.
rpc(
"ledger",
"current");
203 BEAST_EXPECT(
goodLedger(env, ledgerTmp,
"3"));
205 ledgerTmp = env.
rpc(
"ledger",
"4");
206 BEAST_EXPECT(
bad(ledgerTmp));
208 ledgerTmp = env.
rpc(
"ledger",
"100");
209 BEAST_EXPECT(
bad(ledgerTmp));
212 auto lastRotated = firstSeq - 1;
219 ledgerTmp = env.
rpc(
"ledger",
"current");
222 BEAST_EXPECT(store.getLastRotated() == lastRotated);
241 auto ledger = env.
rpc(
"ledger",
"current");
249 lastRotated = store.getLastRotated();
250 BEAST_EXPECT(lastRotated == 11);
258 for (
auto i = lastRotated - 1; i < lastRotated +
deleteInterval - 1;
263 ledgerTmp = env.
rpc(
"ledger",
"current");
269 store.getLastRotated() == lastRotated ||
278 BEAST_EXPECT(store.getLastRotated() ==
deleteInterval + lastRotated);
288 testcase(
"automatic online_delete");
290 using namespace std::chrono_literals;
296 auto lastRotated = ledgerSeq - 1;
297 BEAST_EXPECT(store.getLastRotated() == lastRotated);
298 BEAST_EXPECT(lastRotated != 2);
302 auto const canDelete = env.
rpc(
"can_delete");
310 auto ledger = env.
rpc(
"ledger",
"validated");
320 BEAST_EXPECT(lastRotated == store.getLastRotated());
326 auto ledger = env.
rpc(
"ledger",
"validated");
333 ledgerCheck(env, ledgerSeq - lastRotated, lastRotated);
334 BEAST_EXPECT(lastRotated != store.getLastRotated());
336 lastRotated = store.getLastRotated();
343 auto ledger = env.
rpc(
"ledger",
"validated");
351 BEAST_EXPECT(lastRotated != store.getLastRotated());
357 testcase(
"online_delete with advisory_delete");
359 using namespace std::chrono_literals;
366 auto lastRotated = ledgerSeq - 1;
367 BEAST_EXPECT(store.getLastRotated() == lastRotated);
368 BEAST_EXPECT(lastRotated != 2);
370 auto canDelete = env.
rpc(
"can_delete");
372 BEAST_EXPECT(canDelete[jss::result][jss::can_delete] == 0);
374 canDelete = env.
rpc(
"can_delete",
"never");
376 BEAST_EXPECT(canDelete[jss::result][jss::can_delete] == 0);
379 for (; ledgerSeq < firstBatch; ++ledgerSeq)
383 auto ledger = env.
rpc(
"ledger",
"validated");
391 BEAST_EXPECT(lastRotated == store.getLastRotated());
398 canDelete[jss::result][jss::can_delete] ==
404 BEAST_EXPECT(store.getLastRotated() == lastRotated);
410 auto ledger = env.
rpc(
"ledger",
"validated");
417 ledgerCheck(env, ledgerSeq - lastRotated, lastRotated);
419 BEAST_EXPECT(store.getLastRotated() == ledgerSeq - 1);
420 lastRotated = ledgerSeq - 1;
427 auto ledger = env.
rpc(
"ledger",
"validated");
434 BEAST_EXPECT(store.getLastRotated() == lastRotated);
440 auto ledger = env.
rpc(
"ledger",
"validated");
447 ledgerCheck(env, ledgerSeq - firstBatch, firstBatch);
449 BEAST_EXPECT(store.getLastRotated() == ledgerSeq - 1);
450 lastRotated = ledgerSeq - 1;
453 canDelete = env.
rpc(
"can_delete",
"always");
456 canDelete[jss::result][jss::can_delete] ==
464 auto ledger = env.
rpc(
"ledger",
"validated");
471 BEAST_EXPECT(store.getLastRotated() == lastRotated);
477 auto ledger = env.
rpc(
"ledger",
"validated");
484 ledgerCheck(env, ledgerSeq - lastRotated, lastRotated);
486 BEAST_EXPECT(store.getLastRotated() == ledgerSeq - 1);
487 lastRotated = ledgerSeq - 1;
490 canDelete = env.
rpc(
"can_delete",
"now");
492 BEAST_EXPECT(canDelete[jss::result][jss::can_delete] == ledgerSeq - 1);
499 auto ledger = env.
rpc(
"ledger",
"validated");
506 BEAST_EXPECT(store.getLastRotated() == lastRotated);
512 auto ledger = env.
rpc(
"ledger",
"validated");
519 ledgerCheck(env, ledgerSeq - lastRotated, lastRotated);
521 BEAST_EXPECT(store.getLastRotated() == ledgerSeq - 1);
522 lastRotated = ledgerSeq - 1;
533 boost::filesystem::path newPath;
535 if (!BEAST_EXPECT(
path.size()))
538 section.set(
"path", newPath.string());
555 testcase(
"rotate with lock contention");
566 if (!nscfg.exists(
"cache_size"))
572 if (!nscfg.exists(
"cache_age"))
587 constexpr int readThreads = 4;
591 std::move(writableBackend),
592 std::move(archiveBackend),
598 using namespace std::chrono_literals;
605 auto const cb = [&](
std::string const& writableName,
607 BEAST_EXPECT(writableName ==
"1");
608 BEAST_EXPECT(archiveName ==
"write");
611 BEAST_EXPECT(dbr->getName() ==
"1");
614 dbr->rotate(std::move(newBackend), cb);
616 BEAST_EXPECT(threadNum == 1);
617 BEAST_EXPECT(dbr->getName() ==
"1");
622 auto const cb = [&](
std::string const& writableName,
624 BEAST_EXPECT(writableName ==
"3");
625 BEAST_EXPECT(archiveName ==
"2");
628 BEAST_EXPECT(dbr->getName() ==
"3");
630 auto const cbReentrant = [&](
std::string const& writableName,
632 BEAST_EXPECT(writableName ==
"2");
633 BEAST_EXPECT(archiveName ==
"1");
637 dbr->rotate(std::move(newBackend), cb);
641 dbr->rotate(std::move(newBackend), cbReentrant);
644 BEAST_EXPECT(threadNum == 3);
645 BEAST_EXPECT(dbr->getName() ==
"3");
testcase_t testcase
Memberspace for declaring test cases.
virtual Config & config()=0
virtual SHAMapStore & getSHAMapStore()=0
virtual JobQueue & getJobQueue()=0
virtual RelationalDatabase & getRelationalDatabase()=0
Section & section(std::string const &name)
Returns the section with the given name.
int getValueFor(SizedItem item, std::optional< std::size_t > node=std::nullopt) const
Retrieve the default value for the item at the specified node size.
beast::Journal journal(std::string const &name)
A NodeStore::Scheduler which uses the JobQueue.
virtual std::unique_ptr< Backend > make_Backend(Section const ¶meters, std::size_t burstSize, Scheduler &scheduler, beast::Journal journal)=0
Create a backend.
static Manager & instance()
Returns the instance of the manager singleton.
virtual std::optional< LedgerInfo > getLedgerInfoByIndex(LedgerIndex ledgerSeq)=0
getLedgerInfoByIndex Returns a ledger by its sequence.
class to create database, launch online delete thread, and related SQLite database
virtual void rendezvous() const =0
virtual std::size_t getAccountTransactionCount()=0
getAccountTransactionCount Returns the number of account transactions.
virtual std::size_t getTransactionCount()=0
getTransactionCount Returns the number of transactions.
Holds a collection of configuration values.
void set(std::string const &key, std::string const &value)
Set a key/value pair.
void ledgerCheck(jtx::Env &env, int const rows, int const first)
std::string getHash(Json::Value const &json)
static auto advisoryDelete(std::unique_ptr< Config > cfg)
int waitForReady(jtx::Env &env)
bool bad(Json::Value const &json, error_code_i error=rpcLGR_NOT_FOUND)
void run() override
Runs the suite.
void accountTransactionCheck(jtx::Env &env, int const rows)
static auto onlineDelete(std::unique_ptr< Config > cfg)
std::unique_ptr< NodeStore::Backend > makeBackendRotating(jtx::Env &env, NodeStoreScheduler &scheduler, std::string path)
void transactionCheck(jtx::Env &env, int const rows)
static auto const deleteInterval
bool goodLedger(jtx::Env &env, Json::Value const &json, std::string ledgerID, bool checkDB=false)
A transaction testing environment.
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
bool contains_error(Json::Value const &json)
Returns true if the json contains an rpc error specification.
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
XRP_t const XRP
Converts to XRP Issue or STAmount.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr auto megabytes(T value) noexcept
std::string to_string(base_uint< Bits, Tag > const &a)
static std::string nodeDatabase()
Set the sequence number on a JTx.
T time_since_epoch(T... args)