-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
refactor: consolidate ProTx wallet UTXO locking logic #6536
base: develop
Are you sure you want to change the base?
Conversation
WalkthroughThe pull request introduces modifications to the wallet management functionality in the The modifications include refactoring existing methods like The changes also involve renaming some method parameters for improved clarity and adding a new method ✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (3)
src/wallet/wallet.h (2)
766-771
: Enhance documentation for ret_dups parameter.The documentation should clarify the purpose and behavior of the
ret_dups
parameter, specifically whether it affects the function's behavior or just the return value.- /** Add new UTXOs to the wallet UTXO set - * - * @param[in] tx Transaction to scan eligible UTXOs from - * @param[in] ret_dups Allow UTXOs already in set to be included in return value - * @returns Set of all new UTXOs (eligible to be) added to set */ + /** Add new UTXOs to the wallet UTXO set + * + * @param[in] tx Transaction to scan eligible UTXOs from + * @param[in] ret_dups When true, includes UTXOs already in set in the return value. + * When false, only returns newly added UTXOs. + * @returns Set of UTXOs that were added (if ret_dups=false) or all eligible + * UTXOs from tx (if ret_dups=true) */
1047-1050
: Add documentation for coin management functions.These functions lack documentation explaining their purpose, behavior, and relationships. Documentation is particularly important for the overloaded
ListProTxCoins
to explain the difference between the two variants.+ /** Lists all locked coins in the wallet. + * @param[out] outputs Vector to be populated with locked coin outpoints */ void ListLockedCoins(std::vector<COutPoint>& outputs) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + + /** Lists all ProTx coins in the wallet. + * @param[out] outputs Vector to be populated with ProTx coin outpoints */ void ListProTxCoins(std::vector<COutPoint>& outputs) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + + /** Lists ProTx coins from a specific set of UTXOs. + * @param[in] utxos Set of UTXOs to check for ProTx coins + * @param[out] outputs Vector to be populated with ProTx coin outpoints */ void ListProTxCoins(const std::set<COutPoint>& utxos, std::vector<COutPoint>& outputs) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + + /** Locks all ProTx coins from a specific set of UTXOs. + * @param[in] utxos Set of UTXOs to lock if they are ProTx coins + * @param[in] batch Optional batch to use for writing lock status to database */ void LockProTxCoins(const std::set<COutPoint>& utxos, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);src/wallet/wallet.cpp (1)
1040-1053
: LGTM! Consider adding documentation.The implementation correctly handles UTXO set management with proper locking and duplicate handling. Consider adding documentation to explain the purpose of the
ret_dups
parameter and the function's behavior.Add documentation like:
/** * Add unspent transaction outputs from a transaction to the wallet UTXO set. * @param[in] tx Transaction to add UTXOs from * @param[in] ret_dups Whether to return duplicate UTXOs that are already in the set * @return Set of COutPoints that were added or attempted to be added (if ret_dups=true) */
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/wallet/wallet.cpp
(5 hunks)src/wallet/wallet.h
(2 hunks)
🔇 Additional comments (8)
src/wallet/wallet.h (1)
766-771
: LGTM! The design is clean and consistent.The new UTXO and ProTx management functions are well-integrated into the CWallet class, following existing patterns and maintaining proper thread safety through consistent use of cs_wallet locks.
Also applies to: 1047-1050
src/wallet/wallet.cpp (7)
917-925
: LGTM! Proper UTXO tracking for new transactions.The code correctly tracks UTXOs from newly inserted transactions, using the new AddWalletUTXOs function with ret_dups=true to get all candidates for ProTx locking.
941-942
: LGTM! Proper UTXO tracking for existing transactions.The code correctly tracks UTXOs from existing transactions, using AddWalletUTXOs with ret_dups=false and properly updates the transaction status if new UTXOs are found.
945-946
: LGTM! Proper ProTx locking after UTXO tracking.The code correctly locks ProTx coins using the candidates collected from UTXO tracking, maintaining transactional consistency by using the same batch.
4496-4513
: LGTM! Well-designed ProTx coin listing implementation.The implementation efficiently handles ProTx coin listing with:
- Proper chain validation
- Efficient candidate pair building
- Clear separation between wallet UTXOs and explicit UTXO sets
4515-4522
: LGTM! Proper implementation of ProTx coin locking.The code correctly identifies and locks ProTx coins while maintaining transactional consistency through proper batch usage.
4050-4056
: LGTM! Efficient implementation of automatic masternode collateral locking.The code efficiently collects and locks masternode collaterals using the new UTXO tracking functionality while maintaining proper transaction batching.
4488-4494
: LGTM! Clean implementation of locked coins listing.The implementation correctly handles locked coins listing with proper locking semantics.
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, see comments about:
- std::set vs std::vector + sort + unique
- use RVO instead return by argument
src/wallet/wallet.cpp
Outdated
if (IsMine(wtx.tx->vout[i]) && !IsSpent(hash, i)) { | ||
setWalletUTXO.insert(COutPoint(hash, i)); | ||
outputs.emplace_back(wtx.tx, i); | ||
candidates.emplace(hash, i); |
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.
std::vector usually has significantly better performance (10x), RAM usage is also over-headed (due to bunch of small allocation 32bytes per one item somewhere randomly allocated in heap, instead consequence allocation as vector) compare to std::set.
std::set
has a great usage when you combine 2 types of actions: "add" and "request". In case if you know all elements, you should use data structure with linear memory inside for better performace.
If it is a know case here to have duplicates, consider using std::vector and remove duplicates by std::sort + std::unique + std::erase.
} | ||
} | ||
|
||
void CWallet::ListProTxCoins(std::vector<COutPoint>& vOutpts) const | ||
void CWallet::ListProTxCoins(std::vector<COutPoint>& outputs) const { return ListProTxCoins(setWalletUTXO, outputs); } |
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.
let's return it using RVO here
std::vector<COutPoint> CWallet::ListProTxCoins() const {
return ListProTxCoins(setWalletUTXO);
}
} | ||
} | ||
vOutpts = m_chain->listMNCollaterials(outputs); | ||
outputs = m_chain->listMNCollaterials(candidates); |
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.
let's return here also like RVO:
return m_chain->listMNCollaterials(candidates);
src/wallet/wallet.cpp
Outdated
std::vector<COutPoint> protx_utxos; | ||
ListProTxCoins(candidates, /*output=*/protx_utxos); | ||
for (const auto& utxo : protx_utxos) { |
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.
can be re-written much shorter (see other comments also):
- std::vector<COutPoint> protx_utxos;
- ListProTxCoins(candidates, /*output=*/protx_utxos);
- for (const auto& utxo : protx_utxos) {
+ for (const auto& utxo : ListProTxCoins(candidates)) {
Additional Information
This pull request consolidates locking ProTx UTXOs in a wallet by splitting them into three steps
AddWalletUTXOs()
)ListProTxCoins()
to accept supplied sets)LockProTxCoins()
, which utilizes the previous two steps)These are the behavior changes expected from this PR:
AutoLockMasternodeCollaterals()
will now add new UTXOs it finds to the wallet UTXO set (setWalletUTXO
) (earlier behavior was to only scan and lock).LockCoin()
calls)The
m_chain
checks removed from various sections are acceptable as the check is still present inListProTxCoins()
(source) and it will clear the vector if it fails, which will effectively skip subsequent logic (also because it's the only place wherelistMNCollaterials
is called)ListProTxCoins()
is called but this isn't undesirable.Breaking Changes
None expected.
Checklist