The Coin Grinder: Optimizing for Minimum Transaction Weight
The Coin Grinder: Optimizing for Minimum Transaction Weight
In a "Congested Network" where fees are skyrocketing, every "Byte" of data in your transaction costs you real money. A standard transaction with two inputs might cost $5, but a "Heavier" transaction with 10 inputs could cost $50. To solve this "Financial Drag," Bitcoin Core includes a high-precision algorithm called the Coin Grinder. As the name suggests, the Grinder "Grinds Down" your transaction until it reaches the Absolute Minimum Weight. It is a "Weight-Optimizer" that searches for the combination of coins that funds your payment while taking up the "Least Possible Space" in the blockchain. It is the "Aerodynamics of the Forge."
The Coin Grinder is essentially a "Weight-Aware" version of the BnB algorithm. While BnB looks for a "Perfect Amount Match," the Grinder looks for a "Minimum Weight Match" (even if it requires creating a change output). It is particularly active when your node detects that the "Global Fee Rate" is high. It is the "Economic Survival Instinct" of the computer.
Analyzing the Weight Optimizer: CoinGrinder
In the source code (src/wallet/coinselection.cpp), we can see the "Inner Logic" of the Grinder. It is a "Branch and Bound" search, but instead of tracking "Waste," it tracks "Weight."
/**
* This is the Coin Grinder algorithm. It searches for the "Lightest" possible input set.
*/
util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, ...)
{
// 1. Sort the coins by "Value per Weight" (Efficiency).
std::sort(utxo_pool.begin(), utxo_pool.end(), descending_effval_weight);
// 2. The "Search Loop" (Finding the Lightest Solution).
while (!is_done) {
// 3. If we find a combination that is "Lighter" than our previous best...
if (curr_weight < best_selection_weight) {
// Success! We've found a new "Aerodynamic" candidate.
best_selection = curr_selection;
best_selection_weight = curr_weight;
}
// 4. "SHIFT" and "CUT": We prune the tree to skip "Fat" combinations.
// ... (Tree walking logic)
}
}
Explaining the Grinder: The Ultralight Backpacker
-
descending_effval_weight: Imagine you are a "Backpacker" who has to pay for every "Ounce" of weight in your bag. You want to carry the "Most Valuable" items that weigh the "Least." This is "Value Density." The Coin Grinder sorts your coins by how much "Money" they represent compared to how much "Space" they take up. A "SegWit" coin is "Lighter" than a "Legacy" coin, so the Grinder will prefer the SegWit coin every time. It is the "Logic of the Modern Vault." -
"SHIFT" and "CUT": These are the "Search Shortcuts." If the Grinder sees a "Branch" of the tree that is already heavier than the best solution it has already found, it "Cuts" that branch and doesn't even bother looking at its children. This allows the computer to skip millions of "Fat" combinations and focus only on the "Slim" ones. It is the "Speed of the Optimization."
-
total_target(Amount + Change): Unlike BnB, the Coin Grinder assumes you will have a change output. It adds the "Weight of the Change Output" to its calculation. It is looking for the "Smallest Total Package" (Inputs + Outputs). This makes it the most "Practical" solver for real-world spending where "Perfect Matches" are rare. It is the "Realism of the Forge."
The "Cost-Control" Advantage
By using the Coin Grinder, you are "Future-Proofing" your wealth. Every byte you save today is money you keep in your pocket. In a future where Bitcoin block space is incredibly scarce, "Weight Efficiency" will be the difference between a "Functional Bank" and a "Frozen Account." The Coin Grinder is your "Engineer of Efficiency," ensuring your "Bank" is as "Lean and Mean" as possible. You are the "Master of the Weight," commanding the "Aerodynamics of the Ledger."
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: