1
1
mirror of https://github.com/bitcoin/bitcoin.git synced 2024-05-17 23:56:39 +00:00

refactor: Reduce number of SanityChecks return values

This commit is contained in:
Ryan Ofsky 2022-06-07 21:08:56 -04:00
parent b3e7de7ee6
commit 6db6552377
3 changed files with 10 additions and 26 deletions

View File

@ -1096,21 +1096,8 @@ static bool LockDataDirectory(bool probeOnly)
bool AppInitSanityChecks(const kernel::Context& kernel) bool AppInitSanityChecks(const kernel::Context& kernel)
{ {
// ********************************************************* Step 4: sanity checks // ********************************************************* Step 4: sanity checks
auto maybe_error = kernel::SanityChecks(kernel); if (auto error = kernel::SanityChecks(kernel)) {
InitError(*error);
if (maybe_error.has_value()) {
switch (maybe_error.value()) {
case kernel::SanityCheckError::ERROR_ECC:
InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting."));
break;
case kernel::SanityCheckError::ERROR_RANDOM:
InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting."));
break;
case kernel::SanityCheckError::ERROR_CHRONO:
InitError(Untranslated("Clock epoch mismatch. Aborting."));
break;
} // no default case, so the compiler can warn about missing cases
return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), PACKAGE_NAME)); return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), PACKAGE_NAME));
} }

View File

@ -7,21 +7,22 @@
#include <key.h> #include <key.h>
#include <random.h> #include <random.h>
#include <util/time.h> #include <util/time.h>
#include <util/translation.h>
namespace kernel { namespace kernel {
std::optional<SanityCheckError> SanityChecks(const Context&) std::optional<bilingual_str> SanityChecks(const Context&)
{ {
if (!ECC_InitSanityCheck()) { if (!ECC_InitSanityCheck()) {
return SanityCheckError::ERROR_ECC; return Untranslated("Elliptic curve cryptography sanity check failure. Aborting.");
} }
if (!Random_SanityCheck()) { if (!Random_SanityCheck()) {
return SanityCheckError::ERROR_RANDOM; return Untranslated("OS cryptographic RNG sanity check failure. Aborting.");
} }
if (!ChronoSanityCheck()) { if (!ChronoSanityCheck()) {
return SanityCheckError::ERROR_CHRONO; return Untranslated("Clock epoch mismatch. Aborting.");
} }
return std::nullopt; return std::nullopt;

View File

@ -7,20 +7,16 @@
#include <optional> #include <optional>
struct bilingual_str;
namespace kernel { namespace kernel {
struct Context; struct Context;
enum class SanityCheckError {
ERROR_ECC,
ERROR_RANDOM,
ERROR_CHRONO,
};
/** /**
* Ensure a usable environment with all necessary library support. * Ensure a usable environment with all necessary library support.
*/ */
std::optional<SanityCheckError> SanityChecks(const Context&); std::optional<bilingual_str> SanityChecks(const Context&);
} }