-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Rollback for dumptxoutset without invalidating blocks #33477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33477. ReviewsSee the guideline 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. LLM Linter (✨ experimental)Possible places where named args may be used (e.g.
2025-11-26 |
791471a to
d51c7d7
Compare
|
cc @Sjors since you were asking for this approach a few times :) |
716e1db to
6d409d5
Compare
|
Concept ACK, this seems cleaner.
I suspect if you go back further, this approach will end up performing better because we no longer need to roll back forward at the end |
kevkevinpal
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK 6d409d5
This approach makes more sense. I reviewed the code a bit and made some comments, but nothing blocking
I also added comments on possible functional tests for the new JSONRPCError but these can be done in a followup
src/rpc/blockchain.cpp
Outdated
| "Unless the \"latest\" type is requested, the node will roll back to the requested height and network activity will be suspended during this process. " | ||
| "Because of this it is discouraged to interact with the node in any other way during the execution of this call to avoid inconsistent results and race conditions, particularly RPCs that interact with blockstorage.\n\n" | ||
| "This call may take several minutes. Make sure to use no RPC timeout (bitcoin-cli -rpcclienttimeout=0)", | ||
| "This creates a temporary UTXO database when rolling back, keeping the main chain intact. Should the node experience an unclean shutdown the temporary database may need to be removed from the datadir manually.\n\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be worth noting that "network activity will not be suspended during this process."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is necessary, I don't think a user would naturally assume that there is a reason to suspend network activity for this. Previously we had to do this as basically a hack. When we don't do this anymore it would seem odd to me to mention it.
| CBlock block; | ||
| if (!node.chainman->m_blockman.ReadBlock(block, *block_index)) { | ||
| throw JSONRPCError(RPC_INTERNAL_ERROR, | ||
| strprintf("Failed to read block at height %d", block_index->nHeight)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be able to add a functional test for this rpc error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, this one and the other similar comments about test coverage are all cases that are pretty hard to hit because they should only be possible in a case of db corruption or a similarly unlikely event. Not saying that this wouldn't be valuable coverage, but afaik we hardly ever go through the hassle to cover such cases and this RPC is far from being a critical path in the comparison to the rest of the code base. So, unless you have a specific suggestion for how the can be hit in practical way I would suggest we keep these for a follow-up. The current changes should be a robustness improvement by themselves.
(Marking the other comments as resolved for now but feel free to correct me if you think different about one of them specifically)
| } | ||
| ~TemporaryUTXODatabase() { | ||
| try { | ||
| fs::remove_all(m_path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be useful to add a log here to inform the user the temp UTXO db was cleaned up since we mentioned in logs that we are creating a temp UTXO db
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I don't think we log anything on the creation of the DB so I think I would keep it the same on the destruction. It should only be a debug level log if we would add anything like that since it seems a bit low level for the general user.
|
Nice!
That probably makes sense. It might be possible to do it faster and with less disk usage for relatively short rollbacks via a two step process:
Rather than being direct RPC functionality, maybe it would be better to have an RPC function to export a copy of the utxo set at the current height, and have a separate bitcoin-kernel binary that performs the rollback and utxoset stats calculation itself? |
|
Concept ACK |
|
ACK 6d409d5 This is a good change. Using a temporary coins DB for the rollback is a much cleaner and safer solution than the The code is well-contained, and the new I've pulled the branch, compiled, and the full functional test suite passes, including the |
mzumsande
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK
I suspect if you go back further, this approach will end up performing better because we no longer need to roll back forward at the end
Would be interesting if someone could try out by going back 25k blocks or more, as is usually done for creating snapshots (I can't right now).
| WITH_LOCK(::cs_main, cursor = chainstate.CoinsDB().Cursor()); | ||
|
|
||
| size_t coins_count = 0; | ||
| while (cursor->Valid()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need to hold cs_main throughout the copying phase? What if the utxo set changes while we are copying coins?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so, my understanding is that the cursor/LevelDB iterator works on a snapshot of the DB itself which doesn't get mutated. You can see the same pattern in WriteUTXOSnapshot where we are working with a cursor without holding cs_main as well.
6d409d5 to
b71ee30
Compare
|
Thanks for all the feedback so far and sorry for the slow response!
Hm, feels like a bit overengineered for this functionality, considering the overhead for test coverage and build changes for this. Maybe I am overcomplicating it in my head, I have not done much with kernel yet. But if this is considerably more complex I would rather first go ahead with this and then I would rather keep it for consideration of a future change.
I tried with
I actually had a similar idea early on but then stayed with the simpler approach. I will try this out with a POC and check the performance impact. |
b71ee30 to
53865e7
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. |
Instead this new approach uses a temporary coins db to roll back the UTXO set.
53865e7 to
e0162ab
Compare
I tried to a few different takes on the delta-based idea including some vibe coding tests. Here is the latest one, something is definitely still broken there but the dump it generates is correct. All tests I did had in common that the processing time actually took longer than the current approach, almost 28 min with the latest code version. I am now thinking that longer rollbacks may not be quicker, only shorter ones will be because the big copy overhead in the beginning is saved. But then we also don't have that much to gain. Even if the performance can be improved, now that I have played around with it, it doesn't seem worth the additional code complexity it introduces. It would need to be significantly faster to justify that and I am currently not seeing that. |
This is an alternative approach to implement
dumptxoutsetwith rollback that was discussed a few times. It does not rely oninvalidateblockandreconsiderblockand instead creates a temporary copy of the coins DB, modifies this copy by rolling back as many blocks as necessary and then creating the dump from this temp copy DB. See also #29553 (comment), #32817 (comment) and #29565 discussions.The nice side-effects of this are that forks can not interfere with the rollback and network activity does not have to be suspended. But there are also some downsides when comparing to the current approach: this does require some additional disk space for the copied coins DB and performance is slower (master took 3m 17s vs 9m 16s in my last test with the code here, rolling back ~1500 blocks). However, there is also not much code being added here, network can stay active throughout and performance would stay constant with this approach while it would impact master if there were forks that needed to be invalidated as well (see #33444 for the alternative approach), so this could still be considered a good trade-off.