-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor: move Get*Index to rpc/index_util.cpp, const-ify functions and arguments, add lock annotations and some minor housekeeping #6083
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
Conversation
UdjinM6
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.
LGTM, one suggestion
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.
utACK 13abc0f0c97ad1a3d3048f4c94d9f62dfce2a8c8
src/rpc/misc.cpp
Outdated
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.
| int nHeight; | |
| { | |
| LOCK(::cs_main); | |
| for (const auto& address : addresses) { | |
| if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) { | |
| throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); | |
| } | |
| } | |
| nHeight = chainman.ActiveChain().Height(); | |
| } | |
| int nHeight = [&]() { | |
| LOCK(::cs_main); | |
| for (const auto& address : addresses) { | |
| if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) { | |
| throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); | |
| } | |
| } | |
| return chainman.ActiveChain().Height(); | |
| }(); |
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.
Fetching nHeight and calling GetAddressIndex are two independent actions that happen to access variables protected by the same RecursiveMutex, for that reason they were placed within the same scope. The proposed refactor makes it appears as if running GetAddressIndex is part of the process for fetching nHeight, when it's not.
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.
disagree but nit
src/txmempool.cpp
Outdated
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.
maybe?
| mapSpentIndex::const_iterator it = mapSpent.find(key); | |
| const auto it = mapSpent.find(key); |
|
I'm still confused by all the added cs_mains. You say
and I don't see where cs_main locks are being acquired in develop for these cases. Can you help me figure that out? |
|
This pull request has conflicts, please rebase. |
With bonus const'ing of CTxMemPool::get*Index
With bonus code formatting. We cannot make the functions themselves const as the iterators are non-const.
In Bitcoin Core, the usage of The lack of explicit annotations meant that the safety assumed from the annotations given to functions utilizing The annotation is made explicit in bitcoin#22371 (source, renamed as |
PastaPastaPasta
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.
utACK 8fb8630
UdjinM6
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.
utACK 8fb8630
, bitcoin#23174, bitcoin#23785, bitcoin#23581, bitcoin#23974, bitcoin#22932, bitcoin#24050, bitcoin#24515 (blockstorage backports) 1bf0bf4 merge bitcoin#24515: Only load BlockMan in BlockMan member functions (Kittywhiskers Van Gogh) 5c1eb67 merge bitcoin#24050: Give m_block_index ownership of CBlockIndexes (Kittywhiskers Van Gogh) c440304 merge bitcoin#22932: Add CBlockIndex lock annotations, guard nStatus/nFile/nDataPos/nUndoPos by cs_main (Kittywhiskers Van Gogh) e303a4e merge bitcoin#23974: Make blockstorage globals private members of BlockManager (Kittywhiskers Van Gogh) 301163c merge bitcoin#23581: Move BlockManager to node/blockstorage (Kittywhiskers Van Gogh) 732e871 merge bitcoin#23785: Move stuff to ChainstateManager (Kittywhiskers Van Gogh) b402fd5 merge bitcoin#23174: have LoadBlockIndex account for snapshot use (Kittywhiskers Van Gogh) a08f2f4 merge bitcoin#21526: UpdateTip/CheckBlockIndex assumeutxo support (Kittywhiskers Van Gogh) 472caa0 merge bitcoin#22371: Move pblocktree global to BlockManager (Kittywhiskers Van Gogh) d69ca83 merge bitcoin#21727: Move more stuff to blockstorage (Kittywhiskers Van Gogh) 6df927f chore: exclude underscore placeholder from shadowing linter warnings (Kittywhiskers Van Gogh) Pull request description: ## Additional Information * Dependent on #6078 * Dependent on #6074 * Dependent on #6083 * Dependent on #6119 * Dependency for #6138 * In [bitcoin#24050](bitcoin#24050), `BlockMap` is given ownership of the `CBlockIndex` instance contained within the `unordered_map`. The same has not been done for `PrevBlockMap` as `PrevBlockMap` is populated with `pprev` pointers and doing so seems to break validation logic. * Dash has a specific linter for all Dash-specific code present in Core. The introduction of `util/translation.h` into `validation.h` has caused the linter to trigger shadowing warnings due to a conflict between the common use of `_` as a placeholder/throwaway name ([source](https://github.com/dashpay/dash/blob/37e026a038a60313214e01b6aba029809ea7ad39/src/spork.cpp#L44)) and upstream's usage of it to process translatable strings ([source](https://github.com/dashpay/dash/blob/37e026a038a60313214e01b6aba029809ea7ad39/src/util/translation.h#L55-L62)). Neither C++17 nor C++20 have an _official_ placeholder/throwaway term or annotation for structured bindings (which cannot use `[[maybe_unused]` or `std::ignore`) but [P2169](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2169r4.pdf) is a proposal put forth to make it the official placeholder, in that light, the linter will silence shadowing warnings involving an underscore. ## Breaking Changes None expected ## Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas **(note: N/A)** - [x] I have added or updated relevant unit/integration/functional/e2e tests - [x] I have made corresponding changes to the documentation **(note: N/A)** - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_ ACKs for top commit: UdjinM6: utACK 1bf0bf4 (with one nit) knst: utACK 1bf0bf4 PastaPastaPasta: utACK 1bf0bf4 Tree-SHA512: 875fff34fe91916722f017526135697466e521d7179c473a5c0c444e3aa873369019b804dee9f5f795fc7ebed5c2481b5ce2d895b2950782a37de7b098157ad4
Additional Information
This pull request is motivated by bitcoin#22371, which gets rid of the
pblocktreeglobal.The sole usage of
pblocktreeintroduced by Dash is for managing our {address, spent, timestamp} indexes with most of invocations withinBlockManagerorCChainState, granting them internal access topblocktree(nowm_block_tree_db). The sole exception beingGet*Index, that relies on accessing the global and has no direct internal access.This pull request aims to refactor code associated with
Get*Indexwith the eventual aim of moving gaining access to the global out of the function.Get*Indexis called exclusively called through RPC, which makes giving it access toChainstateManagerquite easy, which makes switching from the global to accessing it throughChainstateManagerwhen backporting bitcoin#22371 a drop-in replacement.Alongside that, the surrounding code has been given some TLC:
validation.cppand intorpc/index_util.cpp. The code is exclusively used in RPC logic and doesn't aid in validation.pblocktree(while already protected by::cs_main, said protection is currently not enforced but will be once moved intoBlockManagerin the backport)const-ing input arguments and using pass-by-value for input arguments that can be written inline (i.e. types likeCSpentIndexKeyare still pass-by-ref).constingCTxMemPoolfunctions were possible (courtesy of the presence ofconst_iterators), the same is currently not possible withCBlockTreeDBfunctions as the iterator is non-const.GetSpentIndexto bring it line with otherGet*Indexes.*Entrytypedef and replacing all explicit type constructions with it.CTxMemPool::getAddressIndexindifferent to howCMempoolAddressDeltaKeyis constructed.std::pair<uint160, AddressType>and construct theCMempoolAddressDeltaKeyinternally, this was presumably done to account for the return type ofgetAddressesFromParamsin the sole call for theCTxMemPool::getAddressIndex.Get*Indexfunctions.Breaking Changes
None expected.
Checklist: