0% found this document useful (0 votes)
272 views11 pages

Summer of Bitcoin Proposal - Bcoin

This document proposes a project to improve coin selection algorithms in the bcoin wallet. Currently, bcoin uses a simple sorting-based algorithm that is memory inefficient and slow. The proposed project would reorganize how UTXOs and transactions are stored, refactor the coin selector to efficiently parse the stored data, and implement the more optimized Branch and Bound coin selection algorithm from Bitcoin Core. This would make coin selection faster and use less memory. The proposal outlines milestones, timeline, and deliverables for the project.

Uploaded by

Rahul Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
272 views11 pages

Summer of Bitcoin Proposal - Bcoin

This document proposes a project to improve coin selection algorithms in the bcoin wallet. Currently, bcoin uses a simple sorting-based algorithm that is memory inefficient and slow. The proposed project would reorganize how UTXOs and transactions are stored, refactor the coin selector to efficiently parse the stored data, and implement the more optimized Branch and Bound coin selection algorithm from Bitcoin Core. This would make coin selection faster and use less memory. The proposal outlines milestones, timeline, and deliverables for the project.

Uploaded by

Rahul Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Implement branch-and-bound

coin selection in bcoin wallet


bcoin.io

Anmol Sharma
1

Indian Institute of Information Technology, Design and Manufacturing, Jabalpur, India

Name and Contact Information


Name: Anmol Sharma
Primary Email: anmolsharma0234@gmail.com
Discord username: theanmolsharma#3037
LinkedIn: theanmolsharma
GitHub: theanmolsharma
Location: Lucknow, India
Timezone: Kolkata, INDIA, UTC+5:30
Typical working hours: 6 – 8 hours from 10 AM to 2 AM (local time)

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.

How will it benefit bcoin users?


1. This project will lay the foundation for the implementation of other coin
selection algorithms by reorganizing the storage of coins in
lib/wallet/txdb.js.
2. This project will implement the Branch and Bound Coin Selection
Algorithm that will improve coin selection in the bcoin wallet.
2

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.

Overview of Bitcoin Core’s Coin Selection Algorithms


Bitcoin Core’s current coin selection consists of multiple steps and is focused on
finding an exact match (input set = target). An exact match avoids the creation of a
change output. An exact match helps in realizing the long-term goal of minimizing
the UTXO set. If Bitcoin Core cannot find an exact match in several attempts, it falls
back to Single Random Draw. Single Random Draw was chosen as it provides a
diverse set of change outputs which should be useful to find more exact matches. If
no exact matches are found, a knapsack solver is used which chooses an input set
that minimizes the change output to a base of 0.01 BTC.

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

Bitcoin Core tries three different ways to find an exact match:

1. With a single UTXO


2. Using Branch and Bound Algorithm to find an exact match
3. By performing Knapsack selection
3

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

Overview of Branch and Bound Selection Algorithm


The Branch and Bound Selection Algorithm is purposefully designed to find exact
matches.

The two main ideas of Branch and Bound are:

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

Overview of Waste Metric


Waste is measured in satoshis and includes the cost of creating change, the excess
selection amount, and the cost of spending inputs now as opposed to sometime in
the future (when we expect to be able to consolidate inputs).

- 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.

Waste = selectionTotal - target + inputs × (currentFeeRate - longTermFeeRate)

Milestones
Milestone 1: Reorganise storage of UTXOs and transactions in lib/wallet/txdb.js.

Milestone 2: Refactor CoinSelector class to efficiently parse coins from txdb.js.

Milestone 3: Implement Branch and Bound Selection Algorithm

TimeLine
Proposal Review Period - April 19, to May 1, 2022

1. Familiarize me completely with the project’s functionality and architecture.


2. Help other contributors if they are stuck when mentors are unavailable.

Program Kick-Off and Onboarding - May 1, to May 23, 2022

1. Start planning the project and milestones with the mentor.


2. Attend talks and seminars on various bitcoin-related topics.
7

Coding Period - May 23, to August 15, 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:

1. Refactor coin selection to efficiently parse coins from lib/wallet/txdb.js.


2. Add necessary tests and document the entire process to make onboarding of
new contributors easy.

Week 7-9:

1. Implement Branch and Bound Selection Algorithm.


2. Add necessary tests and document the entire process to make onboarding of
new contributors easy.

Week 10-11:

1. Develop a testing and benchmarking framework to measure the


performance and efficiency of the algorithm.

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:

1. I will continue contributing to Bcoin by adding more features and helping


maintain the repository after SoB ends.

Mid Term Evaluations - July 4, to July 8, 2022

1. Mentors submit mid-term evaluations of their mentees.

Final Evaluations - August 22, to August 26, 2022


8

1. Mentors do a final review code and determine if the students have


completed their Summer of Bitcoin project.
9

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

Why is this project important to me?


I have been interested in Bitcoin for a long time. I started studying Bitcoin and blockchain in
November 2021. I spent quite some time researching and learning about the Bitcoin network
before diving into the code. Contributing to the project so far has been a great learning
experience for me, and I look forward to making more contributions. I hope to give and take
back a lot through this project.
This project offers me a great opportunity to explore my curiosity about Bitcoin. I am excited to
see how this project might turn out, and how many people’s lives it would impact once all the
goals are accomplished!
As Larry Page once said - “If you’re changing the world, you’re working on important things.
You’re excited to get up in the morning.”
Why me?
I regard myself as a quick learner, who is interested in gaining new knowledge and learning
techniques. I make very informative commit messages which are very important in efficiently
maintaining an Open Source project. I believe it is an art to develop good software and one
needs to be able to think logically as well as be creative to be a good software developer, a
combination of these two qualities makes me a perfect fit for working on this project.

Skills aligned with the project


I am proficient in Javascript and Git and can learn and make use of new libraries/packages
easily. I have ample experience working with Javascript and have made projects ranging from
the “Hello World” program to a full-fledged blogging website with authentication. I also have
good knowledge of various Data Structures and Algorithms and an understanding of
10

algorithmic time and space complexity.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy