Skip to content

Commit d87784a

Browse files
committed
kernel: SanityChecks: Return an error struct
This reduces libbitcoinkernel's coupling with ui_interface and translation.
1 parent 265d639 commit d87784a

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int main(int argc, char* argv[])
5656
// We can't use a goto here, but we can use an assert since none of the
5757
// things instantiated so far requires running the epilogue to be torn down
5858
// properly
59-
assert(kernel::SanityChecks(kernel_context));
59+
assert(!kernel::SanityChecks(kernel_context).has_value());
6060

6161
// Necessary for CheckInputScripts (eventually called by ProcessNewBlock),
6262
// which will try the script cache first and fall back to actually

src/init.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,21 @@ static bool LockDataDirectory(bool probeOnly)
10941094
bool AppInitSanityChecks(const kernel::Context& kernel)
10951095
{
10961096
// ********************************************************* Step 4: sanity checks
1097-
if (!kernel::SanityChecks(kernel)) {
1097+
auto maybe_error = kernel::SanityChecks(kernel);
1098+
1099+
if (maybe_error.has_value()) {
1100+
switch (maybe_error.value()) {
1101+
case kernel::SanityCheckError::ERROR_ECC:
1102+
InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting."));
1103+
break;
1104+
case kernel::SanityCheckError::ERROR_RANDOM:
1105+
InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting."));
1106+
break;
1107+
case kernel::SanityCheckError::ERROR_CHRONO:
1108+
InitError(Untranslated("Clock epoch mismatch. Aborting."));
1109+
break;
1110+
} // no default case, so the compiler can warn about missing cases
1111+
10981112
return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), PACKAGE_NAME));
10991113
}
11001114

src/kernel/checks.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,26 @@
55
#include <kernel/checks.h>
66

77
#include <key.h>
8-
#include <node/ui_interface.h>
98
#include <random.h>
109
#include <util/time.h>
11-
#include <util/translation.h>
12-
13-
#include <memory>
1410

1511
namespace kernel {
1612

17-
bool SanityChecks(const Context&) {
13+
std::optional<SanityCheckError> SanityChecks(const Context&)
14+
{
1815
if (!ECC_InitSanityCheck()) {
19-
return InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting."));
16+
return SanityCheckError::ERROR_ECC;
2017
}
2118

2219
if (!Random_SanityCheck()) {
23-
return InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting."));
20+
return SanityCheckError::ERROR_RANDOM;
2421
}
2522

2623
if (!ChronoSanityCheck()) {
27-
return InitError(Untranslated("Clock epoch mismatch. Aborting."));
24+
return SanityCheckError::ERROR_CHRONO;
2825
}
2926

30-
return true;
27+
return std::nullopt;
3128
}
3229

3330
}

src/kernel/checks.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@
55
#ifndef BITCOIN_KERNEL_CHECKS_H
66
#define BITCOIN_KERNEL_CHECKS_H
77

8+
#include <optional>
9+
810
namespace kernel {
911

1012
struct Context;
1113

14+
enum class SanityCheckError {
15+
ERROR_ECC,
16+
ERROR_RANDOM,
17+
ERROR_CHRONO,
18+
};
19+
1220
/**
1321
* Ensure a usable environment with all necessary library support.
1422
*/
15-
bool SanityChecks(const Context&);
23+
std::optional<SanityCheckError> SanityChecks(const Context&);
1624

1725
}
1826

0 commit comments

Comments
 (0)