The Art of the Group: Grouping Outputs to Avoid Address Reuse
13. The Art of the Group: Grouping Outputs to Avoid Address Reuse
In the "Forge of the Ledger," privacy is often lost through a simple mistake: Address Reuse. If you use the same address to receive money from five different people, those five people can see that those five payments belong to the same person. To prevent this, Bitcoin Core uses a system of "Output Grouping." When the wallet performs "Coin Selection," it doesn't just look at "Individual UTXOs"; it looks at "Groups" of UTXOs that belong to the same address. It treats the "Group" as a single, atomic unit. It is the "Privacy of the Collective."
The logic of grouping is simple: Spend it all or spend nothing. If you own three pieces of gold that were all sent to the same address, the wallet will always try to spend those three pieces together. This prevents the "Partial Spend" leak, where you spend one piece and leave the other two, inadvertently telling the world: "I still have more money at this address!" Grouping is handled by the GroupOutputs function in src/wallet/spend.cpp.
Analyzing the Group Logic: GroupOutputs
In the source code, we see how the wallet "Organizes" the inventory into groups using the "Address" (scriptPubKey) as the key.
/**
* This function "Groups" individual coins by their address pattern.
*/
FilteredOutputGroups GroupOutputs(const CWallet& wallet, ...)
{
// 1. We check the "Avoid Partial Spends" flag (-avoidpartialspends).
if (!coin_sel_params.m_avoid_partial_spends) {
// If disabled, every coin is its own "Group."
return no_grouping;
}
// 2. Create a "Map" of Addresses to "OutputGroups."
std::map<CScript, OutputGroup> group_map;
// 3. We loop through every coin and "Insert" it into the correct group.
for (const COutput& output : coins.All()) {
group_map[output.scriptPubKey].Insert(output);
}
// 4. Return the "Groups" to the Selection Algorithms.
return group_map;
}
Explaining the Group: The Money Bags
-
m_avoid_partial_spends: This is an "Optional Setting" in Bitcoin Core. By default, it is OFF to save you fees (because grouping coins makes transactions "Heavier"). But for the "Sovereign Architect," privacy is often more important than a few satoshis. When you turn this ON, the node starts behaving like a "Privacy-First Bank," refusing to "Split the Identity" of your addresses. It is the "Ethics of the Forge." -
Insert(output): When a group is created, it calculates the "Total Value" and "Total Weight" of all the coins inside it. It also checks the "Mempool Ancestry"—how many "Parents" the group has. If a group has too many unconfirmed parents, it might be "Risky" to spend. TheInsertfunction is the "Auditor of the Group," ensuring the collective is "Healthy." -
scriptPubKey: This is the "Identity" of the address. By grouping byscriptPubKey, the wallet ensures that all coins that "Look the Same" to the outside world are treated as "One Body" by the internal logic. This "Internal Consistency" is what prevents the "External Leak."
The Privacy of the "Identity Block"
By grouping outputs, you are telling the world: "This address is a single, atomic unit of wealth." This makes it much harder for someone to "Peel" your addresses apart and see the individual transactions that created them. Grouping is the "Shield of the Sovereign," the tool that allows you to manage your "Digital Shadows" with precision. You are the "Master of the Group," ensuring your financial "Identity" is never fragmented by the computer's greed for low fees.
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: