Skip to content

wallet, follow-up: Refactor IsSpent to use HowSpent - #35151

Open
musaHaruna wants to merge 1 commit into
bitcoin:masterfrom
musaHaruna:wallet-use-howspent-for-isspent
Open

musaHaruna wants to merge 1 commit into
bitcoin:masterfrom
musaHaruna:wallet-use-howspent-for-isspent

Conversation

@musaHaruna

@musaHaruna musaHaruna commented Apr 24, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #33671

This PR refactors CWallet::IsSpent to reuse the logic already implemented in CWallet::HowSpent.

Instead of manually iterating over mapTxSpends and checking transaction states, IsSpent now simply returns:

return HowSpent(outpoint) != SpendType::UNSPENT;

Also move CWallet::IsSpent from wallet.cpp to wallet.h as a small inline helper, since it is a one-line wrapper around HowSpent.

@DrahtBot

DrahtBot commented Apr 24, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK polespinasa
Concept NACK luke-jr
Concept ACK pablomartin4btc

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #27865 (wallet: Track no-longer-spendable TXOs separately by achow101)

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.

@musaHaruna
musaHaruna force-pushed the wallet-use-howspent-for-isspent branch from f8bab18 to ba4f828 Compare April 24, 2026 13:01
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/24890514354/job/72881342200
LLM reason (✨ experimental): CI failed the trailing_whitespace lint check due to trailing whitespace in src/wallet/wallet.cpp.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@polespinasa polespinasa left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/wallet/wallet.cpp Resolved
Comment thread src/wallet/wallet.cpp Resolved
@musaHaruna
musaHaruna force-pushed the wallet-use-howspent-for-isspent branch from ba4f828 to 8722fad Compare April 24, 2026 15:05
@musaHaruna

Copy link
Copy Markdown
Contributor Author

Force pushed 8722fad

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.

Updated the commit message as suggested.

Given that it is a one-line function I think is fine to remove it from wallet.cpp and add the logic in wallet.h

Move CWallet::IsSpent from wallet.cpp to wallet.h

Comment thread src/wallet/wallet.h Resolved
@musaHaruna
musaHaruna force-pushed the wallet-use-howspent-for-isspent branch from 8722fad to ccb9caf Compare April 24, 2026 15:55
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/24896525104/job/72902929770
LLM reason (✨ experimental): CI failed because the trailing_whitespace lint check detected trailing whitespace in src/wallet/wallet.h (line 569).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@musaHaruna
musaHaruna force-pushed the wallet-use-howspent-for-isspent branch 2 times, most recently from e24b333 to 9775318 Compare April 25, 2026 09:03
@luke-jr

luke-jr commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

Concept NACK. You've made it much less efficient. Not only does it perform more checks, it also doesn't exit the loop early.

@polespinasa

Copy link
Copy Markdown
Member

Not only does it perform more checks

I don't think the extra checks is a performance decrease. If I am not mistaken isConfirmed() and InMempool() only read a value already loaded in CWalletTx.

it also doesn't exit the loop early.

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 HowSpent or only if IsSpent.

    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;
}

@musaHaruna
musaHaruna force-pushed the wallet-use-howspent-for-isspent branch from 64ffa28 to 775964e Compare April 30, 2026 15:40
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task tidy: https://github.com/bitcoin/bitcoin/actions/runs/25172360438/job/73795063472
LLM reason (✨ experimental): CI failed due to a clang-tidy error (readability-avoid-const-params-in-decls) in wallet/wallet.h where parameter isSpent is const-qualified in the declaration.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@musaHaruna musaHaruna left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@polespinasa

Copy link
Copy Markdown
Member

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]>
@musaHaruna
musaHaruna force-pushed the wallet-use-howspent-for-isspent branch from 775964e to ac774b3 Compare May 5, 2026 08:35

@musaHaruna musaHaruna left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@polespinasa polespinasa left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sedited
sedited requested a review from rkrux September 22, 2026 13:35

@pablomartin4btc pablomartin4btc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/wallet/wallet.h
};
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.:

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants