2#include <test/jtx/Env.h>
4#include <xrpl/beast/unit_test.h>
5#include <xrpl/beast/unit_test/suite.h>
6#include <xrpl/beast/utility/Journal.h>
7#include <xrpl/beast/utility/temp_dir.h>
8#include <xrpl/protocol/SField.h>
9#include <xrpl/protocol/jss.h>
11#include <boost/algorithm/string.hpp>
12#include <boost/filesystem.hpp>
27 cfg->START_LEDGER = ledger;
29 cfg->TRAP_TX_HASH = trapTxHash;
30 assert(!dbPath.
empty());
31 cfg->legacy(
"database_path", dbPath);
48 using namespace test::jtx;
56 for (
auto i = 0; i < 20; ++i)
59 env.fund(XRP(10000), acct);
61 if (i > 0 && BEAST_EXPECT(prev))
63 env.trust(acct[
"USD"](1000), *prev);
64 env(pay(acct, *prev, acct[
"USD"](5)));
66 env(offer(acct, XRP(100), acct[
"USD"](1)));
68 prev.emplace(std::move(acct));
71 retval.
ledger = env.rpc(
"ledger",
"current",
"full")[jss::result];
73 retval.
ledger[jss::ledger][jss::accountState].
size() == 102);
76 for (
auto const& it : retval.
ledger[jss::ledger][jss::accountState])
78 if (it[sfLedgerEntryType.fieldName] == jss::LedgerHashes)
79 return it[sfHashes.fieldName];
86 auto const txs = env.rpc(
89 "tx")[jss::result][jss::ledger][jss::transactions];
90 BEAST_EXPECT(txs.isArray() && txs.size() > 0);
92 BEAST_EXPECT(tmp.
parseHex(txs[0u][jss::hash].asString()));
107 using namespace test::jtx;
120 auto jrb = env.rpc(
"ledger",
"current",
"full")[jss::result];
122 sd.
ledger[jss::ledger][jss::accountState].
size() ==
123 jrb[jss::ledger][jss::accountState].size());
130 using namespace test::jtx;
131 using namespace boost::filesystem;
162 boost::system::error_code ec;
163 auto ledgerFileCorrupt =
164 boost::filesystem::path{sd.
dbPath} /
"ledgerdata_bad.json";
168 copy_options::overwrite_existing,
170 if (!BEAST_EXPECTS(!ec, ec.message()))
172 auto filesize = file_size(ledgerFileCorrupt, ec);
173 if (!BEAST_EXPECTS(!ec, ec.message()))
175 resize_file(ledgerFileCorrupt, filesize - 10, ec);
176 if (!BEAST_EXPECTS(!ec, ec.message()))
185 ledgerFileCorrupt.
string(),
197 using namespace test::jtx;
201 boost::erase_all(ledgerHash,
"\"");
212 auto jrb = env.rpc(
"ledger",
"current",
"full")[jss::result];
213 BEAST_EXPECT(jrb[jss::ledger][jss::accountState].size() == 98);
215 jrb[jss::ledger][jss::accountState].size() <=
216 sd.
ledger[jss::ledger][jss::accountState].
size());
222 testcase(
"Load and replay by hash");
223 using namespace test::jtx;
227 boost::erase_all(ledgerHash,
"\"");
238 auto const jrb = env.rpc(
"ledger",
"current",
"full")[jss::result];
239 BEAST_EXPECT(jrb[jss::ledger][jss::accountState].size() == 97);
243 auto const closed = env.rpc(
"ledger",
"current",
"full")[jss::result];
244 BEAST_EXPECT(closed[jss::ledger][jss::accountState].size() == 98);
246 closed[jss::ledger][jss::accountState].size() <=
247 sd.
ledger[jss::ledger][jss::accountState].
size());
253 testcase(
"Load and replay transaction by hash");
254 using namespace test::jtx;
258 boost::erase_all(ledgerHash,
"\"");
269 auto const jrb = env.rpc(
"ledger",
"current",
"full")[jss::result];
270 BEAST_EXPECT(jrb[jss::ledger][jss::accountState].size() == 97);
274 auto const closed = env.rpc(
"ledger",
"current",
"full")[jss::result];
275 BEAST_EXPECT(closed[jss::ledger][jss::accountState].size() == 98);
277 closed[jss::ledger][jss::accountState].size() <=
278 sd.
ledger[jss::ledger][jss::accountState].
size());
284 testcase(
"Load and replay transaction by hash failure");
285 using namespace test::jtx;
289 boost::erase_all(ledgerHash,
"\"");
320 using namespace test::jtx;
329 auto jrb = env.rpc(
"ledger",
"current",
"full")[jss::result];
331 sd.
ledger[jss::ledger][jss::accountState].
size() ==
332 jrb[jss::ledger][jss::accountState].size());
339 using namespace test::jtx;
348 auto jrb = env.rpc(
"ledger",
"current",
"full")[jss::result];
350 sd.
ledger[jss::ledger][jss::accountState].
size() ==
351 jrb[jss::ledger][jss::accountState].size());
373BEAST_DEFINE_TESTSUITE(LedgerLoad, app,
ripple);
UInt size() const
Number of values in array or object.
RAII temporary directory.
std::string path() const
Get the native path for the temporary directory.
std::string file(std::string const &name) const
Get the native path for the a file.
testcase_t testcase
Memberspace for declaring test cases.
bool except(F &&f, String const &reason)
SetupData setupLedger(beast::temp_dir const &td)
void testReplayTx(SetupData const &sd)
void testLoadByHash(SetupData const &sd)
void testLoadIndex(SetupData const &sd)
void run() override
Runs the suite.
void testReplayTxFail(SetupData const &sd)
static auto ledgerConfig(std::unique_ptr< Config > cfg, std::string const &dbPath, std::string const &ledger, Config::StartUpType type, std::optional< uint256 > trapTxHash)
void testBadFiles(SetupData const &sd)
void testLoad(SetupData const &sd)
void testReplay(SetupData const &sd)
void testLoadLatest(SetupData const &sd)
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string to_string(base_uint< Bits, Tag > const &a)