Summer of Bitcoin Proposal - Bcoin
Summer of Bitcoin Proposal - Bcoin
Anmol Sharma
1
Synopsis
This project aims to add more coin selection algorithms to the bcoin wallet. The
bcoin wallet currently uses a simple sorting-based algorithm which is memory
inefficient and slow. This project aims to add more coin selection algorithms that
are memory efficient, optimized for a large number of UTXOs, and optimized for
dust limits and fees. These coin selection algorithms can be directly adopted from
Bitcoin Core.
The project can be divided into three main tasks:
1. Reorganize lib/wallet/txdb.js to efficiently store UTXOs and transactions.
2. CoinSelector class needs to be refactored to parse lib/wallet/txdb.js data
efficiently by only fetching coins that are required instead of pulling all coins.
3. Implement Branch and Bound Coin Selection Algorithm.
Mentors
- Primary mentor - Matthew Zipkin (@pinheadmz)
Deliverables
The main deliverable is to reorganize lib/wallet/txdb.js and implement the Branch and
Bound Coin Selection Algorithm.
Other Coin Selection Algorithms can also be implemented if time permits. If we are unable
to do so in the stipulated time, we can have it as a stretch goal and add them later, post-
SoB.
Although the actual process of coin selection is quite complex, it can be simplified
into three major steps:
1. Fee estimation
2. Exact match attempts (using Branch and Bound)
3. Knapsack Selection
Only when an exact match is not found, only then will it consider sets that would
produce a change output. Finally, it will pick the smaller out of the knapsack result
or the minimum larger UTXO.
4
1. The effective value of UTXOs: Adaptive fee estimation during the selection
is an important feature of the Branch and Bound algorithm as the selection
results in valid results only. Since input scripts and output scripts have fixed
data sizes, the cost per input and output are fixed known values within one
payment. Thus we can calculate the effective value (the contribution of a
UTXO towards a target).
effectiveValue = UTXO.value - feePerByte * bytesPerInput
2. Effective search for exact matches: Instead of repeatedly searching the
same combinations, the combinations of the effective values can be
searched once with less effort. A binary tree is constructed where each level
relates to the inclusion or omission of a UTXO.
Depth-first search (DFS) is used to explore the tree. UTXOs are sorted by their
effective values and the tree is explored deterministically per the inclusion branch
first. At each node, the algorithm checks whether the selection is within the target
range or not. While the selection has not reached the target range, more UTXOs are
included.
When a selection’s value exceeds the target range, the complete subtree deriving
from this selection can be omitted. At that point, the last included UTXO is deleted
and the corresponding omission branch is explored instead. The search ends after
the complete tree has been selected or after a limited number of tries.
The search continues to find better solutions after one solution has been found.
The best solution is chosen by minimizing the waste metric.
The algorithm also uses two additional optimizations. A lookahead keeps track of
the total value of unexplored UTXOs. A subtree is not explored if the lookahead
5
indicates that the target range cannot be reached. This allows for skipping
unnecessary combinations.
6
- Cost of change includes the fees paid on this transaction’s change output
plus the fees that will need to be paid to spend it later. If there is no change
output, the cost is 0.
- Excess selection amount refers to the difference between the sum of
selected inputs and the amount we need to pay (the sum of output values
and fees). There shouldn’t be any excess if there is a change output.
- The cost of spending inputs now is the fee difference between spending
our selected inputs at the current fee rate and spending them later at the
“long-term fee rate.” This helps us implement a long-term fee-minimization
strategy by spending fewer inputs in high fee rate times and consolidating
during low fee rate times.
Milestones
Milestone 1: Reorganise storage of UTXOs and transactions in lib/wallet/txdb.js.
TimeLine
Proposal Review Period - April 19, to May 1, 2022
Week 1-3:
1. Discuss with the mentor, strategies, and ways to reorganize the storage of
UTXOs and transactions in lib/wallet/txdb.js.
2. Implement the discussed changes.
3. Add necessary tests and document the entire process to make onboarding of
new contributors easy.
Week 4-6:
Week 7-9:
Week 10-11:
Week 12:
1. After all the checks and making sure the code is clean, well tested, well
documented, and bug-free, I will submit it for final evaluations.
Post SoB:
About Me
Education
- University: Indian Institute of Information Technology, Design and Manufacturing,
Jabalpur, India
- Major: Electronics and Communications Engineering (B.Tech.)
- Degree Level: 2nd-year undergraduate
- Graduation Year: 2024