Add ledger save/load test (RIPD-1378)

Provide unit test to invoke ledger load at startup.
This commit is contained in:
Mike Ellery
2017-01-04 09:59:07 -08:00
committed by Nik Bougalis
parent fb60cc9b5b
commit be0fb67d8d
5 changed files with 239 additions and 1 deletions

View File

@@ -4168,6 +4168,10 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\test\app\LedgerLoad_test.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\test\app\LoadFeeTrack_test.cpp"> <ClCompile Include="..\..\src\test\app\LoadFeeTrack_test.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>

View File

@@ -4917,6 +4917,9 @@
<ClCompile Include="..\..\src\test\app\HashRouter_test.cpp"> <ClCompile Include="..\..\src\test\app\HashRouter_test.cpp">
<Filter>test\app</Filter> <Filter>test\app</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\test\app\LedgerLoad_test.cpp">
<Filter>test\app</Filter>
</ClCompile>
<ClCompile Include="..\..\src\test\app\LoadFeeTrack_test.cpp"> <ClCompile Include="..\..\src\test\app\LoadFeeTrack_test.cpp">
<Filter>test\app</Filter> <Filter>test\app</Filter>
</ClCompile> </ClCompile>

View File

@@ -0,0 +1,229 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012-2017 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <test/jtx.h>
#include <test/jtx/Env.h>
#include <ripple/beast/unit_test.h>
#include <ripple/protocol/JsonFields.h>
#include <ripple/protocol/SField.h>
#include <boost/scope_exit.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <fstream>
namespace ripple {
class LedgerLoad_test : public beast::unit_test::suite
{
Json::Value jvLedger_;
Json::Value jvHashes_;
boost::filesystem::path ledgerFile_;
boost::filesystem::path dbPath_;
auto ledgerConfig(std::string const& ledger, Config::StartUpType type)
{
assert(! dbPath_.empty());
auto p = std::make_unique<Config>();
test::setupConfigForUnitTests(*p);
p->START_LEDGER = ledger;
p->START_UP = type;
p->legacy("database_path", dbPath_.string());
return p;
}
// setup for test cases
void
setupLedger()
{
using namespace test::jtx;
boost::system::error_code ec;
// create a temporary path to write ledger files in
dbPath_ = boost::filesystem::temp_directory_path(ec);
if(! BEAST_EXPECTS(!ec, ec.message()))
return;
dbPath_ /= boost::filesystem::unique_path("%%%%-%%%%-%%%%-%%%%", ec);
if(! BEAST_EXPECTS(!ec, ec.message()))
return;
boost::filesystem::create_directories(dbPath_, ec);
if(! BEAST_EXPECTS(!ec, ec.message()))
return;
ledgerFile_ = dbPath_ / "ledgerdata.json";
Env env {*this};
Account prev;
for(auto i = 0; i < 20; ++i)
{
Account acct {"A" + std::to_string(i)};
env.fund(XRP(10000), acct);
env.close();
if(i > 0)
{
env.trust(acct["USD"](1000), prev);
env(pay(acct, prev, acct["USD"](5)));
}
env(offer(acct, XRP(100), acct["USD"](1)));
env.close();
prev = std::move(acct);
}
jvLedger_ = env.rpc ("ledger", "current", "full") [jss::result];
BEAST_EXPECT(jvLedger_[jss::ledger][jss::accountState].size() == 101);
for(auto const& it : jvLedger_[jss::ledger][jss::accountState])
{
if(it[sfLedgerEntryType.fieldName] == "LedgerHashes")
{
jvHashes_ = it[sfHashes.fieldName];
}
}
BEAST_EXPECT(jvHashes_.size() == 41);
//write this ledger data to a file.
std::ofstream o (ledgerFile_.string(), std::ios::out | std::ios::trunc);
o << to_string(jvLedger_);
o.close();
}
void
testLoad ()
{
testcase ("Load a saved ledger");
using namespace test::jtx;
// create a new env with the ledger file specified for startup
Env env(*this, ledgerConfig(ledgerFile_.string(), Config::LOAD_FILE));
auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
BEAST_EXPECT(
jvLedger_[jss::ledger][jss::accountState].size() ==
jrb[jss::ledger][jss::accountState].size());
}
void
testBadFiles ()
{
testcase ("Load ledger: Bad Files");
using namespace test::jtx;
using namespace boost::filesystem;
// empty path
except ([this]
{
Env env(*this, ledgerConfig("", Config::LOAD_FILE));
});
// file does not exist
except ([this]
{
Env env(*this, ledgerConfig("badfile.json", Config::LOAD_FILE));
});
// make a corrupted version of the ledger file (last 10 bytes removed).
boost::system::error_code ec;
auto ledgerFileCorrupt = dbPath_ / "ledgerdata_bad.json";
copy_file(
ledgerFile_,
ledgerFileCorrupt,
copy_option::overwrite_if_exists,
ec);
if(! BEAST_EXPECTS(!ec, ec.message()))
return;
auto filesize = file_size(ledgerFileCorrupt, ec);
if(! BEAST_EXPECTS(!ec, ec.message()))
return;
resize_file(ledgerFileCorrupt, filesize - 10, ec);
if(! BEAST_EXPECTS(!ec, ec.message()))
return;
except ([this, &ledgerFileCorrupt]
{
Env env(*this, ledgerConfig(ledgerFileCorrupt.string(), Config::LOAD_FILE));
});
}
void
testLoadByHash ()
{
testcase ("Load by hash");
using namespace test::jtx;
// create a new env with the ledger hash specified for startup
auto ledgerHash = to_string(jvHashes_[jvHashes_.size()-1]);
boost::erase_all(ledgerHash, "\"");
Env env(*this, ledgerConfig(ledgerHash, Config::LOAD));
auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
BEAST_EXPECT(jrb[jss::ledger][jss::accountState].size() == 97);
BEAST_EXPECT(
jrb[jss::ledger][jss::accountState].size() <=
jvLedger_[jss::ledger][jss::accountState].size());
}
void
testLoadLatest ()
{
testcase ("Load by keyword");
using namespace test::jtx;
// create a new env with the ledger "latest" specified for startup
Env env(*this, ledgerConfig("latest", Config::LOAD));
auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
BEAST_EXPECT(
jvLedger_[jss::ledger][jss::accountState].size() ==
jrb[jss::ledger][jss::accountState].size());
}
void
testLoadIndex ()
{
testcase ("Load by index");
using namespace test::jtx;
// create a new env with specific ledger index at startup
Env env(*this, ledgerConfig("43", Config::LOAD));
auto jrb = env.rpc ( "ledger", "current", "full") [jss::result];
BEAST_EXPECT(
jvLedger_[jss::ledger][jss::accountState].size() ==
jrb[jss::ledger][jss::accountState].size());
}
public:
void run ()
{
setupLedger();
BOOST_SCOPE_EXIT( this_ ) {
boost::system::error_code ec;
boost::filesystem::remove_all(this_->dbPath_, ec);
this_->expect(!ec, ec.message(), __FILE__, __LINE__);
} BOOST_SCOPE_EXIT_END
// test cases
testLoad ();
testBadFiles ();
testLoadByHash ();
testLoadLatest ();
testLoadIndex ();
}
};
BEAST_DEFINE_TESTSUITE (LedgerLoad, app, ripple);
} // ripple

View File

@@ -170,7 +170,8 @@ Env::AppBundle::AppBundle(beast::unit_test::suite& suite,
std::move(logs), std::move(timeKeeper_)); std::move(logs), std::move(timeKeeper_));
app = owned.get(); app = owned.get();
app->logs().threshold(beast::severities::kError); app->logs().threshold(beast::severities::kError);
app->setup(); if(! app->setup())
Throw<std::runtime_error> ("Env::AppBundle: setup failed");
timeKeeper->set( timeKeeper->set(
app->getLedgerMaster().getClosedLedger()->info().closeTime); app->getLedgerMaster().getClosedLedger()->info().closeTime);
app->doStart(); app->doStart();

View File

@@ -26,6 +26,7 @@
#include <test/app/Flow_test.cpp> #include <test/app/Flow_test.cpp>
#include <test/app/Freeze_test.cpp> #include <test/app/Freeze_test.cpp>
#include <test/app/HashRouter_test.cpp> #include <test/app/HashRouter_test.cpp>
#include <test/app/LedgerLoad_test.cpp>
#include <test/app/LoadFeeTrack_test.cpp> #include <test/app/LoadFeeTrack_test.cpp>
#include <test/app/MultiSign_test.cpp> #include <test/app/MultiSign_test.cpp>
#include <test/app/OfferStream_test.cpp> #include <test/app/OfferStream_test.cpp>