TeachMeBitcoin

The Strategy of Selection: Introduction to Coin Selection Algorithms

From TeachMeBitcoin, the free encyclopedia Reading time: 4 min

8. The Strategy of Selection: Introduction to Coin Selection Algorithms

Once the "Inventory Report" is complete, the wallet faces its most difficult challenge: Coin Selection. If you want to send 0.5 BTC, and you have 100 different "Pieces of Gold" (UTXOs) totaling 10 BTC, which specific pieces should you use? This is not just a math problem; it is a "Strategy Puzzle." The coins you pick will determine the "Size" of your transaction (and thus the fee), the amount of "Change" you receive back, and the "Privacy" of your financial trail. In Bitcoin Core, this is handled by several sophisticated algorithms working in concert. It is the "Strategy of the Sovereign."

The goal of coin selection is to find the "Best" combination of inputs to satisfy a payment. But "Best" means different things in different contexts. Sometimes "Best" means "Smallest Fee." Sometimes it means "No Change Output" (to save space). Sometimes it means "Maximum Privacy." Bitcoin Core uses a "Hierarchy of Algorithms" to try and satisfy all these goals. It starts with the most efficient and falls back to more robust methods if necessary.

Analyzing the Hierarchy of Choice: AttemptSelection

In the source code (src/wallet/spend.cpp), we can see how the wallet "Cycles" through different strategies. It doesn't just pick one; it tries them all and picks the "Winner" based on a "Waste Metric."

/**
 * This function "Attempts" different coin selection strategies to find the best result.
 */
util::Result<SelectionResult> ChooseSelectionResult(...)
{
    std::vector<SelectionResult> results;

    // 1. Try "Branch and Bound" (BnB).
    // Goal: Find a "Perfect Match" that results in ZERO change.
    if (auto bnb_result = SelectCoinsBnB(...)) {
        results.push_back(*bnb_result);
    }

    // 2. Try "Knapsack Solver."
    // Goal: Solve the "Subset Sum" problem (Robust but may create change).
    if (auto knapsack_result = KnapsackSolver(...)) {
        results.push_back(*knapsack_result);
    }

    // 3. Try "Single Random Draw" (SRD).
    // Goal: A fast, simple fallback that adds privacy through randomness.
    if (auto srd_result = SelectCoinsSRD(...)) {
        results.push_back(*srd_result);
    }

    // 4. We pick the "Winner" with the lowest "Waste."
    return *std::min_element(results.begin(), results.end(), [](const auto& a, const auto& b) {
        return a.GetWaste() < b.GetWaste();
    });
}

Explaining the Strategy: The Shopping Trip

The Sovereign's Choice: Automated vs. Manual

By default, Bitcoin Core handles all of this automatically. But as a "Sovereign Architect," you can override this logic using "Coin Control." You can manually pick the "Pieces of Gold" you want to spend, and the computer will build the transaction exactly as you commanded. This "Manual Override" is the ultimate power of the wallet. It allows you to ignore the "Waste Metric" in favor of your own personal "Privacy Metric." You are the "General of the Coins," the one who makes the final decision on the field of the ledger.


☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!