Repository navigation
wallet, follow-up: Refactor IsSpent to use HowSpent - #35151
musaHaruna wants to merge 1 commit into
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 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. |
f8bab18 to
ba4f828
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. |
There was a problem hiding this comment.
concept ACK
The refactor is nice and clean.
I think it is worth adding in the commit message that this refactor has a super super small performance downgrade, but we can take it as it is negligible. Now we don't early exit if we found any condition that tells us that a coin is spent.
ba4f828 to
8722fad
Compare
|
Force pushed 8722fad
Updated the commit message as suggested.
Move |
8722fad to
ccb9caf
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. |
e24b333 to
9775318
Compare
|
Concept NACK. You've made it much less efficient. Not only does it perform more checks, it also doesn't exit the loop early. |
I don't think the extra checks is a performance decrease. If I am not mistaken
I am not sure how bad it would be to ignore the early exit. But anyway it's easily solvable by adding a bool arg saying if we are checking for the exact SpendType HowSpent(const COutPoint& outpoint, const bool isSpent = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
/** Outpoint is spent if any non-conflicted transaction spends it */
bool IsSpent(const COutPoint& outpoint) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { return HowSpent(outpoint, /*isSpent=*/true) != SpendType::UNSPENT; }CWallet::SpendType CWallet::HowSpent(const COutPoint& outpoint, bool isSpent) const
{
SpendType st{SpendType::UNSPENT};
std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range;
range = mapTxSpends.equal_range(outpoint);
for (TxSpends::const_iterator it = range.first; it != range.second; ++it) {
const Txid& txid = it->second;
const auto mit = mapWallet.find(txid);
if (mit != mapWallet.end()) {
const auto& wtx = mit->second;
if (wtx.isConfirmed()) return SpendType::CONFIRMED;
if (wtx.InMempool()) {
st = SpendType::MEMPOOL;
} else if (!wtx.isAbandoned() && !wtx.isBlockConflicted() && !wtx.isMempoolConflicted()) {
if (st == SpendType::UNSPENT) st = SpendType::NONMEMPOOL;
}
// If we are only checking for spent condition and not how the coin is spent
// early exit as fast as we know if it is spent.
if (isSpent && st != SpendType::UNSPENT) return st;
}
}
return st;
} |
64ffa28 to
775964e
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. |
musaHaruna
left a comment
There was a problem hiding this comment.
Added commit 775964e as suggested by @polespinasa to address the early-exit concern raised in review.
HowSpent now takes an optional isSpent flag, allowing it to return early when used by IsSpent, restoring the previous early-exit behavior while still keeping the refactor.
Thank you so much @polespinasa for the reviews and suggestions.
I will keep the changes like this to see what other reviewers thinks about it.
|
Great! The commits can be squashed into one single commit. Each commit should be good for himself and the first one is a behavior change so it needs the changes in the second :) |
Simplify CWallet::IsSpent by delegating to CWallet::HowSpent. Previously, IsSpent manually iterated over mapTxSpends and applied logic to determine whether an output was spent. This logic is now centralized in HowSpent, which classifies spends into CONFIRMED, MEMPOOL, NONMEMPOOL, or UNSPENT. Introduce an optional `isSpent` flag to HowSpent to allow early exit when only checking whether an outpoint is spent. Move CWallet::IsSpent from wallet.cpp to wallet.h as a small inline helper, since it is a one-line wrapper around HowSpent. Co-authored-by: w0xlt <[email protected]>
775964e to
ac774b3
Compare
musaHaruna
left a comment
There was a problem hiding this comment.
Great!
The commits can be squashed into one single commit. Each commit should be good for himself and the first one is a behavior change so it needs the changes in the second :)
Squashed. Thanks.
There was a problem hiding this comment.
ACK ac774b3
I am not sure about the bool variable name isSpent. It does not self-explain what it does. But anything like only_check_if_spent might be too much.
The comment later in the code makes it clear and because the code function is not large I think this is not a big problem.
pablomartin4btc
left a comment
There was a problem hiding this comment.
Concept ACK
Not strictly a pure refactor: previously a spend counted only if the spending tx was not abandoned, block-conflicted or mempool-conflicted; now a confirmed or in-mempool spending tx counts first. These only differ for a confirmed/ in-mempool tx with non-empty mempool_conflicts, which I don't think can happen in practice (RBF and TRUC sibling eviction both look fine), but it'd be good to mention in the commit message, or add a small unit test checking IsSpent() and HowSpent() != SpendType::UNSPENT agree across tx states.
| }; | ||
| SpendType HowSpent(const COutPoint& outpoint) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); | ||
| bool IsSpent(const COutPoint& outpoint) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); | ||
| SpendType HowSpent(const COutPoint& outpoint, bool isSpent = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); |
There was a problem hiding this comment.
nit: per developer-notes, function args should be snake_case. Also, since with this flag set HowSpent returns as soon as any spend is found (e.g. MEMPOOL even if a later spend is CONFIRMED), the returned SpendType is only meaningful as spent/unspent. Maybe a name that reflects that and a short doc comment, e.g.:
| SpendType HowSpent(const COutPoint& outpoint, bool isSpent = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); | |
| /** If stop_at_first_spend is set, return as soon as any spend is found; the exact SpendType is then not meaningful. */ | |
| SpendType HowSpent(const COutPoint& outpoint, bool stop_at_first_spend = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); |
Alternatively keep the public HowSpent(outpoint) as-is and move the flagged version into a private helper.

Follow-up to #33671
This PR refactors
CWallet::IsSpentto reuse the logic already implemented inCWallet::HowSpent.Instead of manually iterating over mapTxSpends and checking transaction states, IsSpent now simply returns:
return HowSpent(outpoint) != SpendType::UNSPENT;Also move
CWallet::IsSpentfrom wallet.cpp to wallet.h as a small inline helper, since it is a one-line wrapper around HowSpent.