Skip to content

Commit

Permalink
Re-add --output-file to verify-checkpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasBrady committed Sep 27, 2024
1 parent 9fe3310 commit 1c99f55
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 33 deletions.
25 changes: 10 additions & 15 deletions src/historywork/WriteVerifiedCheckpointHashesWork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ WriteVerifiedCheckpointHashesWork::loadHashFromJsonOutput(
}

WriteVerifiedCheckpointHashesWork::WriteVerifiedCheckpointHashesWork(
Application& app, LedgerNumHashPair rangeEnd,
std::string const& trustedHashFile,
Application& app, LedgerNumHashPair rangeEnd, std::string const& outputFile,
std::optional<std::string> const& trustedHashFile,
std::optional<uint32_t> const& fromLedger, uint32_t nestedBatchSize,
std::shared_ptr<HistoryArchive> archive)
: BatchWork(app, "write-verified-checkpoint-hashes")
Expand All @@ -101,7 +101,7 @@ WriteVerifiedCheckpointHashesWork::WriteVerifiedCheckpointHashesWork(
, mCurrCheckpoint(rangeEnd.first)
, mArchive(archive)
, mTrustedHashFileName(trustedHashFile)
, mOutputFileName(mTrustedHashFileName + ".tmp")
, mOutputFileName(outputFile)
, mFromLedger(fromLedger)
{
mRangeEndPromise.set_value(mRangeEnd);
Expand Down Expand Up @@ -238,13 +238,13 @@ WriteVerifiedCheckpointHashesWork::startOutputFile()
void
WriteVerifiedCheckpointHashesWork::maybeParseTrustedHashFile()
{
if (mFromLedger)
if (mFromLedger || !mTrustedHashFileName)
{
return;
}
mLatestTrustedHashPair =
loadLatestHashPairFromJsonOutput(mTrustedHashFileName);
CLOG_INFO(History, "trusted hash from {}: {}", mTrustedHashFileName,
loadLatestHashPairFromJsonOutput(*mTrustedHashFileName);
CLOG_INFO(History, "trusted hash from {}: {}", *mTrustedHashFileName,
hexAbbrev(*mLatestTrustedHashPair->second));
}

Expand All @@ -253,11 +253,12 @@ WriteVerifiedCheckpointHashesWork::endOutputFile()
{
if (mOutputFile && mOutputFile->is_open())
{
if (std::filesystem::exists(mTrustedHashFileName))
if (mTrustedHashFileName &&
std::filesystem::exists(*mTrustedHashFileName))
{
// Append everything except the first line of mTrustedHashFile to
// mOutputFile.
std::ifstream trustedHashFile(mTrustedHashFileName);
std::ifstream trustedHashFile(*mTrustedHashFileName);
if (trustedHashFile)
{
std::string line;
Expand All @@ -273,7 +274,7 @@ WriteVerifiedCheckpointHashesWork::endOutputFile()
else
{
CLOG_WARNING(History, "failed to open trusted hash file {}",
mTrustedHashFileName);
*mTrustedHashFileName);
}
}
else
Expand All @@ -287,12 +288,6 @@ WriteVerifiedCheckpointHashesWork::endOutputFile()
}
mOutputFile->close();
mOutputFile.reset();
// Rename mOutputFileName to mTrustedHashFileName.
if (std::rename(mOutputFileName.c_str(), mTrustedHashFileName.c_str()))
{
CLOG_ERROR(History, "failed to rename {} to {}", mOutputFileName,
mTrustedHashFileName);
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/historywork/WriteVerifiedCheckpointHashesWork.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class WriteVerifiedCheckpointHashesWork : public BatchWork
public:
WriteVerifiedCheckpointHashesWork(
Application& app, LedgerNumHashPair rangeEnd,
std::string const& trustedHashFile,
std::string const& outputFile,
std::optional<std::string> const& trustedHashFile,
std::optional<uint32_t> const& fromLedger,
uint32_t nestedBatchSize = NESTED_DOWNLOAD_BATCH_SIZE,
std::shared_ptr<HistoryArchive> archive = nullptr);
Expand Down Expand Up @@ -84,7 +85,7 @@ class WriteVerifiedCheckpointHashesWork : public BatchWork
void startOutputFile();
void endOutputFile();
std::shared_ptr<std::ofstream> mOutputFile;
std::string const mTrustedHashFileName;
std::optional<std::string> const mTrustedHashFileName;
std::string const mOutputFileName;
std::optional<LedgerNumHashPair> mLatestTrustedHashPair;
std::optional<uint32_t> const& mFromLedger;
Expand Down
28 changes: 17 additions & 11 deletions src/historywork/test/HistoryWorkTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,33 @@ TEST_CASE("write verified checkpoint hashes", "[historywork]")
LedgerNumHashPair pair = pairs.back();
auto tmpDir = catchupSimulation.getApp().getTmpDirManager().tmpDir(
"write-checkpoint-hashes-test");
std::string const file = tmpDir.getName() + "/verified-ledgers.json";
std::string file = tmpDir.getName() + "/verified-ledgers.json";
auto& wm = catchupSimulation.getApp().getWorkScheduler();
std::optional<std::uint32_t> noFromLedger = std::nullopt;
std::optional<std::string> noVerifiedLedgerFile = std::nullopt;

size_t startingPair = 0;
size_t startingPairIdx = 0;
{
SECTION("from genesis")
{
auto w = wm.executeWork<WriteVerifiedCheckpointHashesWork>(
pair, file, noFromLedger, nestedBatchSize);
pair, file, noVerifiedLedgerFile, noFromLedger,
nestedBatchSize);
REQUIRE(w->getState() == BasicWork::State::WORK_SUCCESS);
}
SECTION("from specified ledger")
{
startingPair = 1;
std::optional<std::uint32_t> fromLedger = pairs[startingPair].first;
startingPairIdx = 1;
std::optional<std::uint32_t> fromLedger =
pairs[startingPairIdx].first;
auto w = wm.executeWork<WriteVerifiedCheckpointHashesWork>(
pair, file, fromLedger, nestedBatchSize);
pair, file, noVerifiedLedgerFile, fromLedger, nestedBatchSize);
REQUIRE(w->getState() == BasicWork::State::WORK_SUCCESS);
}
}

auto checkFileContents = [&](const auto& pairs) {
auto checkFileContents = [](const auto& pairs, auto startingPairIdx,
std::string file) {
for (size_t i = 0; i < pairs.size(); ++i)
{
auto p = pairs[i];
Expand All @@ -65,7 +69,7 @@ TEST_CASE("write verified checkpoint hashes", "[historywork]")
p.first, file);
// If we did not start from the beginning, the hashes before the
// starting pair should not be in the file.
if (i < startingPair)
if (i < startingPairIdx)
{
REQUIRE(h == Hash{});
}
Expand All @@ -82,7 +86,7 @@ TEST_CASE("write verified checkpoint hashes", "[historywork]")
REQUIRE(latest->first == pairs.back().first);
};

checkFileContents(pairs);
checkFileContents(pairs, startingPairIdx, file);

// Advance the simulation.
auto secondCheckpointLedger =
Expand All @@ -91,15 +95,17 @@ TEST_CASE("write verified checkpoint hashes", "[historywork]")
5 * nestedBatchSize);
pairs = catchupSimulation.getAllPublishedCheckpoints();

std::optional<std::string> trustedHashFile = file;
file += ".new";
// Run work again with existing file.
{
auto w = wm.executeWork<WriteVerifiedCheckpointHashesWork>(
pairs.back(), file, noFromLedger, nestedBatchSize);
pairs.back(), file, trustedHashFile, noFromLedger, nestedBatchSize);
REQUIRE(w->getState() == BasicWork::State::WORK_SUCCESS);
}

// Ensure the file contains all pairs, from the first run and the second.
checkFileContents(pairs);
checkFileContents(pairs, startingPairIdx, file);
}

TEST_CASE("check single ledger header work", "[historywork]")
Expand Down
18 changes: 13 additions & 5 deletions src/main/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,10 @@ outputFileParser(std::string& string)
}

clara::Opt
trustedHashFileParser(std::string& string)
trustedHashFileParser(std::optional<std::string>& string)
{
return clara::Opt{string, "FILE-NAME"}["--trusted-hash-file"](
return clara::Opt{[&](std::string const& arg) { string = arg; },
"FILE-NAME"}["--trusted-hash-file"](
"file containing trusted hashes, generated by a previous call to "
"verify-checkpoints or a non-existent file to generate a new one");
}
Expand Down Expand Up @@ -1029,7 +1030,8 @@ runPublish(CommandLineArgs const& args)
int
runWriteVerifiedCheckpointHashes(CommandLineArgs const& args)
{
std::string trustedHashFile;
std::string outputFile;
std::optional<std::string> trustedHashFile;
uint32_t startLedger = 0;
std::string startHash;
std::optional<std::uint32_t> fromLedger;
Expand All @@ -1038,8 +1040,14 @@ runWriteVerifiedCheckpointHashes(CommandLineArgs const& args)
args,
{configurationParser(configOption), historyLedgerNumber(startLedger),
historyHashParser(startHash), fromLedgerNumberParser(fromLedger),
trustedHashFileParser(trustedHashFile).required()},
trustedHashFileParser(trustedHashFile),
outputFileParser(outputFile).required()},
[&] {
if (fromLedger && trustedHashFile)
{
LOG_ERROR(DEFAULT_LOG, "Cannot specify both --from-ledger and "
"--trusted-hash-file");
}
VirtualClock clock(VirtualClock::REAL_TIME);
auto cfg = configOption.getConfig();

Expand All @@ -1066,7 +1074,7 @@ runWriteVerifiedCheckpointHashes(CommandLineArgs const& args)
app->getHerder().shutdown();
app->getWorkScheduler()
.executeWork<WriteVerifiedCheckpointHashesWork>(
authPair, trustedHashFile, fromLedger);
authPair, outputFile, trustedHashFile, fromLedger);
app->gracefulStop();
return 0;
}
Expand Down

0 comments on commit 1c99f55

Please sign in to comment.