-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ledger save/load test (RIPD-1378) #1955
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/support/jtx.h> | ||
#include <test/support/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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Part of me wants you to make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see your point - what about just adding an assert() to verify dbPath_ is set? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that's certainly simpler. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. idk about all that, the rules for test code are more relaxed compared to production code. We should not worry about "accidental" calls. For unit tests, we prioritize convenience and compactness of notation over robustness |
||
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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC, building a new
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure - will use the move |
||
|
||
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to not compare |
||
} | ||
|
||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment does not match code. Maybe can get rid of comment. Or |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's a possible replacement with the same functionality but no macros:
Tested, seems to work fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...and we can change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so. The deleter has to take a single There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it - thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a little frustrating that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C++17 auto deduction will then get rid of the need for
|
||
|
||
// test cases | ||
testLoad (); | ||
testBadFiles (); | ||
testLoadByHash (); | ||
testLoadLatest (); | ||
testLoadIndex (); | ||
} | ||
}; | ||
|
||
BEAST_DEFINE_TESTSUITE (LedgerLoad, app, ripple); | ||
|
||
} // ripple |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
#include
may be redundant.