Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed May 29, 2024
1 parent f62f663 commit 2b7b7f7
Showing 1 changed file with 140 additions and 8 deletions.
148 changes: 140 additions & 8 deletions src/test/app/LedgerLoad_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
//==============================================================================

#include <ripple/beast/unit_test.h>
#include <ripple/beast/unit_test/suite.h>
#include <ripple/beast/utility/Journal.h>
#include <ripple/beast/utility/temp_dir.h>
#include <ripple/protocol/SField.h>
#include <ripple/protocol/jss.h>
Expand All @@ -36,10 +38,12 @@ class LedgerLoad_test : public beast::unit_test::suite
std::unique_ptr<Config> cfg,
std::string const& dbPath,
std::string const& ledger,
Config::StartUpType type)
Config::StartUpType type,
std::optional<uint256> trapTxHash)
{
cfg->START_LEDGER = ledger;
cfg->START_UP = type;
cfg->TRAP_TX_HASH = trapTxHash;
assert(!dbPath.empty());
cfg->legacy("database_path", dbPath);
return cfg;
Expand All @@ -52,6 +56,7 @@ class LedgerLoad_test : public beast::unit_test::suite
std::string ledgerFile{};
Json::Value ledger{};
Json::Value hashes{};
uint256 trapTxHash{};
};

SetupData
Expand Down Expand Up @@ -94,6 +99,16 @@ class LedgerLoad_test : public beast::unit_test::suite
}();

BEAST_EXPECT(retval.hashes.size() == 41);
retval.trapTxHash = [&]() {
auto const txs = env.rpc(
"ledger",
std::to_string(41),
"tx")[jss::result][jss::ledger][jss::transactions];
BEAST_EXPECT(txs.isArray() && txs.size() > 0);
uint256 tmp;
BEAST_EXPECT(tmp.parseHex(txs[0u][jss::hash].asString()));
return tmp;
}();

// write this ledger data to a file.
std::ofstream o(retval.ledgerFile, std::ios::out | std::ios::trunc);
Expand All @@ -112,7 +127,11 @@ class LedgerLoad_test : public beast::unit_test::suite
Env env(
*this,
envconfig(
ledgerConfig, sd.dbPath, sd.ledgerFile, Config::LOAD_FILE),
ledgerConfig,
sd.dbPath,
sd.ledgerFile,
Config::LOAD_FILE,
std::nullopt),
nullptr,
beast::severities::kDisabled);
auto jrb = env.rpc("ledger", "current", "full")[jss::result];
Expand All @@ -132,7 +151,12 @@ class LedgerLoad_test : public beast::unit_test::suite
except([&] {
Env env(
*this,
envconfig(ledgerConfig, sd.dbPath, "", Config::LOAD_FILE),
envconfig(
ledgerConfig,
sd.dbPath,
"",
Config::LOAD_FILE,
std::nullopt),
nullptr,
beast::severities::kDisabled);
});
Expand All @@ -142,7 +166,11 @@ class LedgerLoad_test : public beast::unit_test::suite
Env env(
*this,
envconfig(
ledgerConfig, sd.dbPath, "badfile.json", Config::LOAD_FILE),
ledgerConfig,
sd.dbPath,
"badfile.json",
Config::LOAD_FILE,
std::nullopt),
nullptr,
beast::severities::kDisabled);
});
Expand Down Expand Up @@ -172,7 +200,8 @@ class LedgerLoad_test : public beast::unit_test::suite
ledgerConfig,
sd.dbPath,
ledgerFileCorrupt.string(),
Config::LOAD_FILE),
Config::LOAD_FILE,
std::nullopt),
nullptr,
beast::severities::kDisabled);
});
Expand All @@ -189,7 +218,12 @@ class LedgerLoad_test : public beast::unit_test::suite
boost::erase_all(ledgerHash, "\"");
Env env(
*this,
envconfig(ledgerConfig, sd.dbPath, ledgerHash, Config::LOAD),
envconfig(
ledgerConfig,
sd.dbPath,
ledgerHash,
Config::LOAD,
std::nullopt),
nullptr,
beast::severities::kDisabled);
auto jrb = env.rpc("ledger", "current", "full")[jss::result];
Expand All @@ -199,6 +233,99 @@ class LedgerLoad_test : public beast::unit_test::suite
sd.ledger[jss::ledger][jss::accountState].size());
}

void
testReplay(SetupData const& sd)
{
testcase("Load and replay by hash");
using namespace test::jtx;

// create a new env with the ledger hash specified for startup
auto ledgerHash = to_string(sd.hashes[sd.hashes.size() - 1]);
boost::erase_all(ledgerHash, "\"");
Env env(
*this,
envconfig(
ledgerConfig,
sd.dbPath,
ledgerHash,
Config::REPLAY,
std::nullopt),
nullptr,
beast::severities::kDisabled);
auto const jrb = env.rpc("ledger", "current", "full")[jss::result];
BEAST_EXPECT(jrb[jss::ledger][jss::accountState].size() == 97);

env.close();
auto const closed = env.rpc("ledger", "current", "full")[jss::result];
BEAST_EXPECT(closed[jss::ledger][jss::accountState].size() == 98);
BEAST_EXPECT(
closed[jss::ledger][jss::accountState].size() <=
sd.ledger[jss::ledger][jss::accountState].size());
}

void
testReplayTx(SetupData const& sd)
{
testcase("Load and replay transaction by hash");
using namespace test::jtx;

// create a new env with the ledger hash specified for startup
auto ledgerHash = to_string(sd.hashes[sd.hashes.size() - 1]);
boost::erase_all(ledgerHash, "\"");
Env env(
*this,
envconfig(
ledgerConfig,
sd.dbPath,
ledgerHash,
Config::REPLAY,
sd.trapTxHash),
nullptr,
beast::severities::kDisabled);
auto const jrb = env.rpc("ledger", "current", "full")[jss::result];
BEAST_EXPECT(jrb[jss::ledger][jss::accountState].size() == 97);

env.close();
auto const closed = env.rpc("ledger", "current", "full")[jss::result];
BEAST_EXPECT(closed[jss::ledger][jss::accountState].size() == 98);
BEAST_EXPECT(
closed[jss::ledger][jss::accountState].size() <=
sd.ledger[jss::ledger][jss::accountState].size());
}

void
testReplayTxFail(SetupData const& sd)
{
testcase("Load and replay transaction by hash failure");
using namespace test::jtx;

// create a new env with the ledger hash specified for startup
auto ledgerHash = to_string(sd.hashes[sd.hashes.size() - 1]);
boost::erase_all(ledgerHash, "\"");
try
{
Env env(
*this,
envconfig(
ledgerConfig,
sd.dbPath,
ledgerHash,
Config::REPLAY,
~sd.trapTxHash),
nullptr,
beast::severities::kDisabled);
BEAST_EXPECT(false);
}
catch (std::runtime_error)
{
BEAST_EXPECT(true);
}
catch (...)
{
BEAST_EXPECT(false);
}
}

void
testLoadLatest(SetupData const& sd)
{
Expand All @@ -208,7 +335,8 @@ class LedgerLoad_test : public beast::unit_test::suite
// create a new env with the ledger "latest" specified for startup
Env env(
*this,
envconfig(ledgerConfig, sd.dbPath, "latest", Config::LOAD),
envconfig(
ledgerConfig, sd.dbPath, "latest", Config::LOAD, std::nullopt),
nullptr,
beast::severities::kDisabled);
auto jrb = env.rpc("ledger", "current", "full")[jss::result];
Expand All @@ -226,7 +354,8 @@ class LedgerLoad_test : public beast::unit_test::suite
// create a new env with specific ledger index at startup
Env env(
*this,
envconfig(ledgerConfig, sd.dbPath, "43", Config::LOAD),
envconfig(
ledgerConfig, sd.dbPath, "43", Config::LOAD, std::nullopt),
nullptr,
beast::severities::kDisabled);
auto jrb = env.rpc("ledger", "current", "full")[jss::result];
Expand All @@ -246,6 +375,9 @@ class LedgerLoad_test : public beast::unit_test::suite
testLoad(sd);
testBadFiles(sd);
testLoadByHash(sd);
testReplay(sd);
testReplayTx(sd);
testReplayTxFail(sd);
testLoadLatest(sd);
testLoadIndex(sd);
}
Expand Down

0 comments on commit 2b7b7f7

Please sign in to comment.