Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
|
Concept ACK :) |
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
Interesting. I didn't know this was a goal. Has this been discussed somewhere? Do we want to have no ability for the kernel to show detailed translated messages when something fails in the kernel, like "Bad undo data in block {hash} height {height} file {filename}" (to pick a random example)? Is this because we want (1) all kernel messages to be untranslated? Or (2) all messages that are translated to be represented as structs and enums in the kernel and made into strings by applications? Or some combination of these approaches? I do think these approaches are reasonable, but they do come with costs. Approach (1) makes harder to write polished applications that are localized and do not require english. Approach (2) is costly to developers and users because it either results in vague messages that do not include important details necessary to understand, or it requires complicated structural representations of exceptional conditions and statuses in code. IMO, util/translation.h is a low overhead way to provide helpful error and status messages that can be easily translated as needed. I don't see significant benefits to removing it and don't think it should be a goal to remove. Which is not to say that providing structural error information is not useful at all. Right now we do not provide enough structural error information and should provide more. But it is also possible to take things too far and add too much structure. We should not be creating a situation where calling any kernel API means needing to handle a half-dozen error codes, when a simple error status and a translated string would be easier to handle in code and provide more detailed and helpful information to users. |
|
@ryanofsky thanks for sharing your opinion! I don't know if this has been discussed in more depth than the checklist item in #27587 from i took inspiration. @sedited might be able to answer that better. If more discussion is needed, we can keep it here on the PR, or I (or someone else, not sure about the process) can start a thread on the mailing list, whatever works best. Given the two directions you describe, I was thinking on the 2 when writing the PR. I think this is less about the dependency itself (despite removing it is a nice win for me), On the complexity you are worried about: this set is small and closed. There are 18 cases in total right now, 16 On your example, "Bad undo data in block {hash} height {height} file {filename}", option (2) does not have to produce a vaguer message. On "calling any kernel API means needing to handle a half-dozen error codes": I see that as a positive. Handling the error would be the application's responsibility, and I think that is what lets the kernel stay smaller and closer to consensus, further away from logging or presenting errors. The C API just hands back the numeric code ( All of the above is just my own opinion. As a fairly recent contributor, I am not trying to impose any of this, just making proposals I am happy to change and discuss. |
|
Thanks, and I want apologize because my last comment was just based on an initial reaction and not very constructive. I do agree with the direction of the PR and approach of adding structured error types in a src/kernel/error.h file. Right now kernel functions don't return enough structured error information and they should return more. I just want to caution against trying to use types intended for error handling for error reporting as well, because the goals of error handling and error reporting are different. For error handling you generally want fewer branching cases to avoid code complexity and bugs, while for error reporting you usually want more detail. I was also just surprised to see the goal of removing the translation header because it is so small and it was unclear what benefits that removal would bring. Overall direction of the PR seems good, but would maybe just want to clarify the goals a little more. I think there are a number of problems worth addressing in the error handling area, the main ones being that errors are returned by callback instead of return value, which makes them difficult to handle, and that we don't have good error type, and to extend we do there is no type safety so a function that can actually only return 3 errors looks like it returns 15 errors and caller have to deal with unnecessary ambiguity and mess. But looking at actual changes in this PR they seem reasonable, and like something that could be built on to fix other problems. The one thing I would change is that it seems like btck_NotifyFatalError and btck_NotifyFlushError are losing all details about errors that occur because messages are being replaced by codes. This seems like it could cause real harm to kernel applications because there could be no clean way to extract and present error information to users. But could be fixed by keeping the error messages and adding the codes instead of replacing the messages with codes |
|
Appreciate that, @ryanofsky. I have updated the PR description, hope it is what you wanted. I would also like to recommend reviewers take a look at #35646 (I just left a review there): although DrahtBot does not flag it as a conflict, both PRs define a |
|
Could turn into draft while CI is red? |
Translation is an application concern, so the node builds the message. The C API callback takes the error code alone.
Same as the previous commit, with each error's dynamic context carried as typed fields. std::visit keeps the node's message table exhaustive. node/blockstorage.cpp no longer needs util/translation.h.
Moves the m_shutdown_on_fatal_error check into a single place, so that node code aborting without a kernel error can reuse it.
init.cpp is not part of libbitcoinkernel, so no consumer of the library could ever be notified of this error; it now aborts through AbortNode() directly.
It was forwarded as a plain string, so the node could only re-emit it untranslated; InvalidateCoinsDBOnDisk() now returns the two paths and the node builds the message, which also takes the last _() out of the kernel's fatal error paths.
1d66b88 to
8a5934f
Compare
| @@ -0,0 +1,86 @@ | |||
| // Copyright (c) The Bitcoin Core developers | |||
There was a problem hiding this comment.
af5ed18 commit message can be more descriptive.
| assert(false); | ||
| } | ||
|
|
||
| btck_FlushError cast_btck_flush_error(kernel::FlushError error) |
There was a problem hiding this comment.
can be marked constexpr & named to_btck to match naming convention?
| assert(false); | ||
| } | ||
|
|
||
| btck_FatalError cast_btck_fatal_error(const kernel::FatalError& error) |
|
|
||
| btck_FatalError cast_btck_fatal_error(const kernel::FatalError& error) | ||
| { | ||
| return std::visit(util::Overloaded{ |
There was a problem hiding this comment.
I think with this change this method reduces each variant to an error code losing the error details (block hash, paths, snapshot recovery info). For e.g. SystemErrorWhileSavingBlock now reports only SYSTEM_ERROR_WHILE_SAVING_BLOCK consumers cannot reconstruct the underlying cause from the callback.
Maybe exposing through some error object could be a way, not sure though!
There was a problem hiding this comment.
Also the heights are also gone from the kernel's log. Because cdd4757 drops the [snapshot] !!! LogError in handle_invalid_snapshot so then after a hash mismatch C API users can't get either height from the callback or the log.
Could the kernel keep a plain log line with them? Something like LogError("[snapshot] validation failed, resetting chain height from %d to %d", ...), similar to the assumeutxo data not found for height (%d) warning just below. Which would be diagnostic rather than user-facing, so it shouldn't need _().
To check, I added ASSERT_DEBUG_LOG("[snapshot] !!!") to chainstatemanager_snapshot_completion_hash_mismatch. The test passes on f739c78 and 54bc6a5 and fails on 8a5934f.
| } | ||
|
|
||
| bool FatalError(Notifications& notifications, BlockValidationState& state, const bilingual_str& message) | ||
| std::string FatalErrorString(const kernel::FatalError& error) |
There was a problem hiding this comment.
This can now return an empty string if the variant isn’t one of the listed types, which wasn’t possible before. In practice, though, every caller passes one of the listed types.
| i2p_tests.cpp | ||
| interfaces_tests.cpp | ||
| key_io_tests.cpp | ||
| kernel_error_tests.cpp |
There was a problem hiding this comment.
should come before key_io_tests.cpp?
|
FatalErrorString in src/validation.cpp returns {} for the eight variants that are only routed through Notifications::fatalError(). Having an Assume(false) before the fallthrough return {}; would catch any accidental use of those variants in FatalError(notifications, state, ...) in debug builds, preventing BlockValidationState::Error() from silently absorbing an empty reject reason. For btck_NotifyFatalError, passing both the code and the formatted message would mirror btck_NotifyWarningSet, which already supplies both btck_Warning warning and (const char* message, size_t message_len). That preserves programmatic dispatch for C consumers without dropping dynamic details like paths or block hashes. |
| } | ||
|
|
||
| void FlushErrorHandler(std::string_view error) override | ||
| void FlushErrorHandler(FlushError error) override |
There was a problem hiding this comment.
Genuinely curious, would it make sense to add a test_kernel case for it? Destroying the chainman triggers a flush, and replacing blocks/blk00000.dat (or rev00000.dat) with a directory is enough to make that flush fail. I ran it on 8a5934f and the handler got BLOCK_FILE_FLUSH_FAILED (0) and UNDO_FILE_FLUSH_FAILED (1).
test_kernel.cpp diff
+class FlushErrorRecorder : public TestKernelNotifications
+{
+public:
+ std::vector<FlushError> m_errors;
+ void FlushErrorHandler(FlushError error) override { m_errors.push_back(error); }
+};
+
+static std::vector<FlushError> DestroyWithUnopenableFile(const std::string& name, const std::string& file)
+{
+ auto test_directory{TestDirectory{name}};
+ auto notifications{std::make_shared<FlushErrorRecorder>()};
+ auto context{create_context(notifications, ChainType::REGTEST)};
+ auto chainman{create_chainman(
+ test_directory, /*reindex=*/false, /*wipe_chainstate=*/false,
+ /*block_tree_db_in_memory=*/true, /*chainstate_db_in_memory=*/true, context)};
+ for (size_t i{0}; i < 3; i++) {
+ Block block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[i])};
+ bool new_block{false};
+ chainman->ProcessBlock(block, &new_block);
+ BOOST_REQUIRE(new_block);
+ }
+ const fs::path path{test_directory.m_directory / "blocks" / fs::u8path(file)};
+ BOOST_REQUIRE(fs::exists(path));
+ fs::rename(path, path + ".moved");
+ BOOST_REQUIRE(fs::create_directory(path));
+ BOOST_REQUIRE(notifications->m_errors.empty());
+ chainman.reset();
+ return notifications->m_errors;
+}
+
+static int RawValue(FlushError error) { return int{static_cast<std::underlying_type_t<FlushError>>(error)}; }
+
+BOOST_AUTO_TEST_CASE(btck_flush_error_block_file)
+{
+ const auto errors{DestroyWithUnopenableFile("flush_error_block_file", "blk00000.dat")};
+ BOOST_REQUIRE_EQUAL(errors.size(), 1U);
+ BOOST_CHECK(errors[0] == FlushError::BLOCK_FILE_FLUSH_FAILED);
+ BOOST_CHECK_EQUAL(RawValue(errors[0]), 0);
+}
+
+BOOST_AUTO_TEST_CASE(btck_flush_error_undo_file)
+{
+ const auto errors{DestroyWithUnopenableFile("flush_error_undo_file", "rev00000.dat")};
+ BOOST_REQUIRE_EQUAL(errors.size(), 1U);
+ BOOST_CHECK(errors[0] == FlushError::UNDO_FILE_FLUSH_FAILED);
+ BOOST_CHECK_EQUAL(RawValue(errors[0]), 1);
+}|
|
||
| btck_FatalError cast_btck_fatal_error(const kernel::FatalError& error) | ||
| { | ||
| return std::visit(util::Overloaded{ |
There was a problem hiding this comment.
Also the heights are also gone from the kernel's log. Because cdd4757 drops the [snapshot] !!! LogError in handle_invalid_snapshot so then after a hash mismatch C API users can't get either height from the callback or the log.
Could the kernel keep a plain log line with them? Something like LogError("[snapshot] validation failed, resetting chain height from %d to %d", ...), similar to the assumeutxo data not found for height (%d) warning just below. Which would be diagnostic rather than user-facing, so it shouldn't need _().
To check, I added ASSERT_DEBUG_LOG("[snapshot] !!!") to chainstatemanager_snapshot_completion_hash_mismatch. The test passes on f739c78 and 54bc6a5 and fails on 8a5934f.
| #define btck_FatalError_BLOCK_WRITE_FAILED ((btck_FatalError)(5)) | ||
| #define btck_FatalError_CORRUPT_BLOCK_FOUND ((btck_FatalError)(6)) | ||
| #define btck_FatalError_DISK_SPACE_TOO_LOW ((btck_FatalError)(7)) | ||
| #define btck_FatalError_FAILED_TO_START_INDEXES ((btck_FatalError)(8)) |
There was a problem hiding this comment.
Could 6daa91c and the init.cpp change here go before af5ed18? Then FailedToStartIndexes is never added and codes 9 to 16 keep their values, as the "appended, never inserted" doc comment from cdd4757 says.
8a5934f could likewise be folded into cdd4757, since it restores the translation that cdd4757 drops from the rename message.
nit: If you keep this commit: the message says init calls AbortNode() directly, but it goes through KernelNotifications::abort().

Part of the libbitcoinkernel project (tracking issue #27587 ), specifically the "Remove translation.h usage from the kernel" item.
kernel::Notifications::fatalError()andflushError()take abilingual_str, which pullsutil/translation.hand the application-levelG_TRANSLATION_FUNinto the kernel library, and translation is an application concern rather than a consensus one. The same interface already shows the way out:warningSet()takes akernel::Warningenum, and the node turns it into a translated string innode/kernel_notifications.cpp.Along with removing the dependency, another goal here is separating responsibilities, because the goals of error handling and error reporting are different: presenting an error to a user is an application concern, not something
libbitcoinkernelitself should own. The error is structured on both sides: in C++ as the fullkernel::FatalErrorvariant, and in the C API as its numeric value. With the current changes the C side only carries half of that structure today though, the dynamic fields (a block hash, a path, an exception'swhat()) do not cross the C API yet, even though ideally they should.This PR does the same for the two error notifications. A new
src/kernel/error.hdefineskernel::FlushErroras a plain enum andkernel::FatalErroras astd::variantwith one struct per error, whose fields carry the context the message needs: a block hash, a path, thewhat()of a caught exception, block heights. It holds types and nothing else, so every message is now built on the node side. The exception isFatalErrorString()invalidation.cpp, because a fatal error raised during validation also fails the block throughBlockValidationState::Error(), which takes the reason as a plain string right there, andfatalError()returns void, so the node has no way to put its message into that state. A follow-up could store the typed error in the state instead and let the node create the string message.This does not remove the kernel's dependency on
util/translation.h, it is just a step. The other two notifications,progress()andwarningSet(), still take abilingual_str, andbitcoinkernel.cpp,checks.cpp,node/chainstate.cpp,txmempool.cppandvalidation.cppstill include the header.There is one behavior change to mention here. The two C API callbacks lose their
messageparameter, so they now carry only the error code, the same pattern thatbtck_BlockValidationResultalready uses.