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 ConflictsNo conflicts as of last run. |
|
🚧 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. |
e1bf41a to
2c1edcc
Compare
2c1edcc to
b2c824a
Compare
a8cf396 to
1829b35
Compare
rkrux
left a comment
There was a problem hiding this comment.
Started reviewing; I'm not opposed to this introduction at the moment and I will get more clarity soon in the review.
Initial Concept ACK 1829b3512d7bf430ec44daa801eb8fadf46cd30b
These 3 commits from #33034 looks pretty generic and related enough. Can they be included in this PR?
- 818989093d9e6b8ee765c21630f7342b99af7fa0 "sqlite: Make SQLiteStatement::Bind a template function"
- 07cac74e1340c7e771850633dd9418ff3efa5bb7 "sqlite: Make SQLiteDatabase::Column an std::optional"
- ff47eea527e58071bdaff5c2acbc98ee97294021 "sqlite: Add additional blob types to SQLiteStatement::Column"
vasild
left a comment
There was a problem hiding this comment.
Approach ACK 1829b3512d7bf430ec44daa801eb8fadf46cd30b
| int res = sqlite3_prepare_v2(m_db, stmt_text.c_str(), -1, &m_stmt, nullptr); | ||
| if (res != SQLITE_OK) { | ||
| throw std::runtime_error(strprintf( |
There was a problem hiding this comment.
Just an observation:
Some of the callers of sqlite3_prepare_v2() call sqlite3_finalize() on failure. The doc says that if prepare fails then *ppStmt is set to NULL and Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
So it is ok to omit the finalize call.
| ~SQLiteStatement() | ||
| { | ||
| Reset(); | ||
| sqlite3_finalize(m_stmt); |
There was a problem hiding this comment.
Some of the callers of sqlite3_finalize() used to check its return value. Here in the destructor it is too late to signal an error to the caller.
If the most recent evaluation of statement S failed, then sqlite3_finalize(S) returns the appropriate error code or extended error code
I am not sure what is the best way to handle this. Ignoring the return value does not look good.
Maybe assert that sqlite3_finalize() returns SQLITE_OK? And also somehow make sure that if "the most recent evaluation of statement S failed", then this is handled already, before the destructor is called. That would mean if sqlite3_step(), called from Step() fails (result is not SQLITE_ROW or SQLITE_DONE) then to call sqlite3_finalize() from inside Step()?
There was a problem hiding this comment.
Maybe (not tested):
int Step()
{
const int ret = sqlite3_step(m_stmt);
if (ret == SQLITE_ROW || ret == SQLITE_DONE) {
return ret;
}
sqlite3_finalize(m_stmt);
m_stmt = nullptr;
// then the destructor can assert(sqlite3_finalize(m_stmt) == SQLITE_OK);
}Related to #33033 (comment)
There was a problem hiding this comment.
If the most recent evaluation of statement S failed, then sqlite3_finalize(S) returns the appropriate error code or extended error code
I am not sure what is the best way to handle this. Ignoring the return value does not look good.
This text reads to me as sqlite3_finalize propagating any previous errors, e.g. from sqlite3_step. Since any errors from that should already be handled, I think it's ok to ignore the result of finalize. I think the previous code is actually incorrect since the error result is not actually a result of finalize failing.
There was a problem hiding this comment.
Yeah, I was thinking the same. It is just super odd to ignore database errors (in general). I guess the sqlite API is odd here.
Edit: if you retouch, maybe change int Step() to [[nodiscard]] int Step() to hint the callers to not ignore the return value.
There was a problem hiding this comment.
+1 This should be marked nodiscard
1829b35 to
0af7c2a
Compare
vasild
left a comment
There was a problem hiding this comment.
ACK 0af7c2a11819a21e11abf34b4229c5c21150d827
0af7c2a to
637770c
Compare
|
Ping @davidgumberg @vasild for hopefully another round of review. |
pablomartin4btc
left a comment
There was a problem hiding this comment.
ACK 1682818
Left some inline nits. Two broader observations I think worth noting:
-
GetNewPrefixCursorbind failure is now a throw instead ofnullptr-
PreviouslyGetNewPrefixCursorreturnednullptrwhenBindBlobToStatementfailed, and callers handled that. Now theSQLiteCursorconstructor throwsstd::runtime_errorif either bind call fails, and no caller catches it — so it propagates up the stack. Also, since this PR introduces the behaviour change, wouldn't be natural to clean up the now-dead null-checks inwalletdb.cpp(lines 193, 470, 523)? -
sqlite3_prepare_v2ignores trailing SQL (pzTail) -
TheSQLiteStatementconstructor passesnullptras thepzTailout-parameter, so ifstmt_textever contained more than one statement (e.g. a stray semicolon —"INSERT INTO main VALUES(?,?); DROP TABLE main"), SQLite would silently prepare only the first and discard the rest (@Eunovo raised it also).
| error += Untranslated(strprintf("%s\n", str_msg)); | ||
| } | ||
| error += Untranslated(strprintf("%s\n", str_msg)); | ||
| } catch (std::runtime_error& e) { |
| } else if constexpr (std::is_same_v<T, std::string>) { | ||
| const char* text = (const char*)sqlite3_column_text(m_stmt, col); | ||
| if (!text) { | ||
| return ""; |
There was a problem hiding this comment.
worth considering a LogWarning or at least a doc comment?
There was a problem hiding this comment.
No. A column that stores a string could contain nothing in it if there is no string to store, which is equivalent to the empty string.
1682818 to
c31076e
Compare
|
c31076e to
4ba5654
Compare
|
🚧 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. |
|
|
||
| ~SQLiteStatement() | ||
| { | ||
| sqlite3_finalize(m_stmt); |
There was a problem hiding this comment.
in 4ace00b sqlite: Add SQLiteStatement RAII class
I think is worth to disallow copies? Similar as DatabaseBatch does in db.h? A copy would double finalize m_stmt (I think that would crash).
SQLiteStatement(const SQLiteStatement&) = delete;
SQLiteStatement& operator=(const SQLiteStatement&) = delete;
| } else if constexpr (std::integral<T> && std::is_signed_v<T> && sizeof(T) <= 8) { | ||
| return static_cast<int64_t>(sqlite3_column_int64(m_stmt, col)); |
There was a problem hiding this comment.
in 244554b sqlite: Make Column template function
I think the static_cast is a no-op. sqlite3_column_int64 already returns an int_64.
Also isn't sizeof(T) <= 8 also a no-op?
Maybe:
| } else if constexpr (std::integral<T> && std::is_signed_v<T> && sizeof(T) <= 8) { | |
| return static_cast<int64_t>(sqlite3_column_int64(m_stmt, col)); | |
| } else if constexpr (std::integral<T> && std::is_signed_v<T>) { | |
| static_assert(sizeof(T) <= 8, "T is larger than 8bytes and would be truncated."); | |
| return sqlite3_column_int64(m_stmt, col); | |
| `` |
There was a problem hiding this comment.
I think the static_cast is a no-op. sqlite3_column_int64 already returns an int_64.
It's a sqlite3_int64 which is a typedef of long long int. The static cast is to put it into a type that we actually use.
Also isn't
sizeof(T) <= 8also a no-op?
Possibly, but it doesn't hurt.
| void SetupSQLStatements(); | ||
| bool ExecStatement(SQLiteStatement* stmt, std::span<const std::byte> blob); | ||
| bool ExecStatement(SQLiteStatement* stmt); | ||
| bool ExecEraseStatement(SQLiteStatement* stmt, std::span<const std::byte> blob); |
There was a problem hiding this comment.
in 84dd0c3 sqlite: Refactor common writing code from WriteKey and ExecStatement
You mention in the commit message that ExecStatement is renamed to ExecEraseStatement, but this last is never defined in .cpp.
This class will be used to encapsulate a sqlite3_stmt
WriteKey and ExecStatement use the same code for the actual execution of the statement.
Instead of constructing all SQLiteStatements when SQLiteBatch is constructed, construct them once when they are needed before each read, write, or erase operation. Once constructed, the statement will persist for the lifetime of the SQLiteBatch to be reused across multiple statements.
4ba5654 to
774fbc8
Compare
pablomartin4btc
left a comment
There was a problem hiding this comment.
re-ACK 774fbc8
Thanks for addressing my nits.
Also since my previous review, @Eunovo and @polespinasa feedback were addressed.
I went through some findings from Ralph and verified the following 3 against the code, which may be worth fixing if you retouch:
ExecStatementreintroducesLogErrorand the trailing\nthat master changed toLogWarningin fa45a15;m_cursor_stmtis declared beforem_prefix_range_start/m_prefix_range_end, so the vectors (bound withSQLITE_STATIC) are destroyed before the statement is finalized; declaring it after them would fix that;SpanFromBlobused brace-init, which guaranteedsqlite3_column_blobis evaluated beforesqlite3_column_bytes; the inlinedT(...)doesn't, whileT{...}would.
Every SQLiteBatch prepared all five of its statements in the constructor, and since a connected block opens a batch per wallet, plus short batches such as WriteBestBlock and AddToWallet, profiling IBD with a SQLite wallet showed 10.2% of the scheduler thread's on-CPU samples in sqlite3_prepare_v2 under SQLiteBatch::SetupSQLStatements(). Most batches use one or two of the statements, so prepare each one when the batch first uses it. Preparing still throws on failure, now from the first read or write instead of from MakeBatch. Upstream bitcoin#33033 (open) makes the same change as part of a larger refactor.

SQLite statements are C objects which can be long lived, so having them be initialized in a constructor, and cleanup everything in a destructor, makes the sqlite code cleaner and easier to read. This also lets us have the various functions needed to interact with sqlite statements be member functions.
This will be particularly useful in future work which makes use of more complicated SQL statements.