forked from bitcoin/bitcoin
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5ad49ad
refactor: move Get{Address*, Timestamp, Spent}Index out of validation
kwvg 625982e
refactor: make CTxMemPool::get*Index functions and arguments const
kwvg 9a6503d
refactor: make CBlockTreeDB::Read*Index arguments const
kwvg 488f047
rpc: extend error-on-failure to GetSpentIndex
kwvg 808842b
refactor: define all key/value pairings as *Entry and use them instead
kwvg ee9d112
refactor: move pair value swapping out of CTxMemPool::getAddressIndex()
kwvg 3e0fcf4
refactor: move accessing CBlockTreeDB global out of Get*Index
kwvg 8fb8630
refactor: inline sorting and make available through argument
kwvg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Copyright (c) 2016 BitPay, Inc. | ||
| // Copyright (c) 2024 The Dash Core developers | ||
| // Distributed under the MIT software license, see the accompanying | ||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
|
||
| #include <rpc/index_util.h> | ||
|
|
||
| #include <txmempool.h> | ||
| #include <uint256.h> | ||
| #include <validation.h> | ||
|
|
||
| bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, | ||
| std::vector<CAddressIndexEntry>& addressIndex, | ||
| const int32_t start, const int32_t end) | ||
| { | ||
| AssertLockHeld(::cs_main); | ||
|
|
||
| if (!fAddressIndex) | ||
| return error("Address index not enabled"); | ||
|
|
||
| if (!block_tree_db.ReadAddressIndex(addressHash, type, addressIndex, start, end)) | ||
| return error("Unable to get txids for address"); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, | ||
| std::vector<CAddressUnspentIndexEntry>& unspentOutputs, const bool height_sort) | ||
| { | ||
| AssertLockHeld(::cs_main); | ||
|
|
||
| if (!fAddressIndex) | ||
| return error("Address index not enabled"); | ||
|
|
||
| if (!block_tree_db.ReadAddressUnspentIndex(addressHash, type, unspentOutputs)) | ||
| return error("Unable to get txids for address"); | ||
|
|
||
| if (height_sort) { | ||
| std::sort(unspentOutputs.begin(), unspentOutputs.end(), | ||
| [](const CAddressUnspentIndexEntry &a, const CAddressUnspentIndexEntry &b) { | ||
| return a.second.m_block_height < b.second.m_block_height; | ||
| }); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool, | ||
| const std::vector<CMempoolAddressDeltaKey>& addressDeltaIndex, | ||
| std::vector<CMempoolAddressDeltaEntry>& addressDeltaEntries, | ||
| const bool timestamp_sort) | ||
| { | ||
| if (!fAddressIndex) | ||
| return error("Address index not enabled"); | ||
|
|
||
| if (!mempool.getAddressIndex(addressDeltaIndex, addressDeltaEntries)) | ||
| return error("Unable to get address delta information"); | ||
|
|
||
| if (timestamp_sort) { | ||
| std::sort(addressDeltaEntries.begin(), addressDeltaEntries.end(), | ||
| [](const CMempoolAddressDeltaEntry &a, const CMempoolAddressDeltaEntry &b) { | ||
| return a.second.m_time < b.second.m_time; | ||
| }); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool GetSpentIndex(CBlockTreeDB& block_tree_db, const CTxMemPool& mempool, const CSpentIndexKey& key, | ||
| CSpentIndexValue& value) | ||
| { | ||
| AssertLockHeld(::cs_main); | ||
|
|
||
| if (!fSpentIndex) | ||
| return error("Spent index not enabled"); | ||
|
|
||
| if (mempool.getSpentIndex(key, value)) | ||
| return true; | ||
|
|
||
| if (!block_tree_db.ReadSpentIndex(key, value)) | ||
| return error("Unable to get spend information"); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool GetTimestampIndex(CBlockTreeDB& block_tree_db, const uint32_t high, const uint32_t low, | ||
| std::vector<uint256>& hashes) | ||
| { | ||
| AssertLockHeld(::cs_main); | ||
|
|
||
| if (!fTimestampIndex) | ||
| return error("Timestamp index not enabled"); | ||
|
|
||
| if (!block_tree_db.ReadTimestampIndex(high, low, hashes)) | ||
| return error("Unable to get hashes for timestamps"); | ||
|
|
||
| return true; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Copyright (c) 2016 BitPay, Inc. | ||
| // Copyright (c) 2024 The Dash Core developers | ||
| // Distributed under the MIT software license, see the accompanying | ||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
|
||
| #ifndef BITCOIN_RPC_INDEX_UTIL_H | ||
| #define BITCOIN_RPC_INDEX_UTIL_H | ||
|
|
||
| #include <cstdint> | ||
| #include <vector> | ||
|
|
||
| #include <amount.h> | ||
| #include <addressindex.h> | ||
| #include <spentindex.h> | ||
| #include <sync.h> | ||
| #include <threadsafety.h> | ||
|
|
||
| class CBlockTreeDB; | ||
| class CTxMemPool; | ||
| class uint160; | ||
| class uint256; | ||
|
|
||
| enum class AddressType : uint8_t; | ||
|
|
||
| extern RecursiveMutex cs_main; | ||
|
|
||
| bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, | ||
| std::vector<CAddressIndexEntry>& addressIndex, | ||
| const int32_t start = 0, const int32_t end = 0) | ||
| EXCLUSIVE_LOCKS_REQUIRED(::cs_main); | ||
| bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, | ||
| std::vector<CAddressUnspentIndexEntry>& unspentOutputs, const bool height_sort = false) | ||
| EXCLUSIVE_LOCKS_REQUIRED(::cs_main); | ||
| bool GetMempoolAddressDeltaIndex(const CTxMemPool& mempool, | ||
| const std::vector<CMempoolAddressDeltaKey>& addressDeltaIndex, | ||
| std::vector<CMempoolAddressDeltaEntry>& addressDeltaEntries, | ||
| const bool timestamp_sort = false); | ||
| bool GetSpentIndex(CBlockTreeDB& block_tree_db, const CTxMemPool& mempool, const CSpentIndexKey& key, | ||
| CSpentIndexValue& value) | ||
| EXCLUSIVE_LOCKS_REQUIRED(::cs_main); | ||
| bool GetTimestampIndex(CBlockTreeDB& block_tree_db, const uint32_t high, const uint32_t low, | ||
| std::vector<uint256>& hashes) | ||
| EXCLUSIVE_LOCKS_REQUIRED(::cs_main); | ||
|
|
||
| #endif // BITCOIN_RPC_CLIENT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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
nHeightand callingGetAddressIndexare two independent actions that happen to access variables protected by the sameRecursiveMutex, for that reason they were placed within the same scope. The proposed refactor makes it appears as if runningGetAddressIndexis part of the process for fetchingnHeight, 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