算法艺术创作
algorithmic-art
通过代码和可调参数创作生成艺术与交互式视觉效果。
bitcoin-coin-selection【设计与创意】Coin selection algorithms: branch-and-bound (BnB), knapsack, single random draw (SRD), Murch's waste metric, change avoidance. Determines which UTXOs to spend for a given target. USE WHEN: implementing wallet send flow, choosing which UTXOs to consolidate, optimizing fees, designing privacy-preserving selection.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Coin selection chooses which subset of available UTXOs to spend for a target amount + fee. Trade-offs: fee minimization, privacy, change-output dust avoidance, UTXO-set hygiene.
T).r sat/vB).{ (value_i, vsize_when_spent_i) }.Bitcoin Core's primary algorithm (Murch 2017).
Goal: find a subset that sums exactly to T + fee + change_buffer,
avoiding change output entirely.
branch_and_bound(utxos, target):
utxos.sort_by(value, descending)
best = None
recurse(included=[], remaining=utxos, sum=0):
if sum > target + threshold: return # exceed
if abs(sum - target) <= waste_metric:
best = included; return
if remaining empty: return
# branch: include / exclude next utxo
recurse(included + [head], tail, sum + head.value)
recurse(included, tail, sum)
Backtracking with pruning. If BnB finds an exact-match solution (within rounding), no change output needed → saves the cost of a change output (~31 vB for P2WPKH change).
If BnB fails (no exact match), wallet falls back to knapsack or SRD.
Heuristic: iterate UTXOs, include each if it fits, else continue. Simple but may select more inputs than necessary. Pre-Murch default.
Randomly draw UTXOs until sum ≥ target + fee. Privacy-friendly: no heuristic-readable patterns from chain analysis.
srd(utxos, target):
shuffle(utxos)
selected = []; sum = 0
for u in utxos:
selected.push(u); sum += u.value
if sum >= target + fee_for_count(len(selected)): break
return selected
Defines "best" selection as one that minimizes:
waste = excess + change_cost + (input_count - 1) * fee_per_input
excess = sum(selected) - target - change.change_cost = vsize of change output × fee rate (cost of the
change output) + future cost to spend that change later.Wallets use this to compare candidate solutions.
If sum(selected) is within ~$change_threshold$ of target (typically
~1000 sats above), spend everything as fee instead of creating dusty
change. Algorithmic threshold = change_cost.
When fee rates are low, batch many UTXOs into one output. Saves future tx fees but reveals on-chain history correlation.
Heuristics chain analysts use:
Privacy-focused wallets (Wasabi, Sparrow with PayJoin) use SRD to avoid heuristic-friendly patterns.
| Wallet | Algorithm |
|---|---|
| Bitcoin Core | BnB primary, SRD fallback, knapsack legacy |
| BDK | BnB + SRD configurable |
| Sparrow | BnB + SRD + privacy-aware |
| Wasabi | Privacy-first with random draws + CoinJoin pre-step |