<h2>Introduction</h2>

<p>The key to finding out where a piece of code is without already knowing where it is is to start at the thing that will eventually lead to what you want to find. These can be logically thought through. For example, for relay and validation, these all occur after a node has received a block or transaction, so begin at the point where a block or transaction is received.</p>

<p>There are generally three kinds of actions that Bitcoin Core does: validation and relay of blocks and transactions, the wallet, and startup</p>

<h2>Validation and relay</h2>

<p>In order for Bitcoin Core to be able to validate and relay a block or transaction, it has to first receive it. So logically the place to begin looking is at the point where a block or transaction has been received and it is beginning to be processed. That is in <a href="https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/net_processing.cpp#L1774" rel="noreferrer"><code>ProcessMessages</code> function in <code>src/net_processing.cpp</code></a>. Within this function, there are several <code>if</code> statements for each type of network message that can be received.</p>

<p>For transactions, you want to look at the <code>if</code> block for <a href="https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/net_processing.cpp#L2393" rel="noreferrer"><code>NetMsgType::TX</code></a>. By reading through the code in this <code>if</code> block and following the functions that are called, you will eventually reach where a transaction is verified, added to the mempool, and relayed. The most important function within this block is <code>AcceptToMemoryPool</code> and that is where all of the validation for an unconfirmed transaction is done.</p>

<p>For blocks, you want to look at the <code>if</code> block for <a href="https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/net_processing.cpp#L2773" rel="noreferrer"><code>NetMsgType::BLOCK</code></a>. Reading through this code will lead you to <a href="https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L3559" rel="noreferrer"><code>ProcessNewBlock</code></a> and later <a href="https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L2651" rel="noreferrer"><code>ActivateBestChain</code></a> and <a href="https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L2401" rel="noreferrer"><code>ConnectTip</code></a> which are the functions that contain the validation of blocks.</p>

<h2>The wallet</h2>

<p>Almost all of the wallet's functionality is centered around the creation and receiving of transactions. These logically begin when the user wants to send Bitcoin. So a good starting place is the <a href="https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/wallet/rpcwallet.cpp#L351" rel="noreferrer"><code>sendtoaddress</code> RPC</a>. Following this function will take you to <a href="https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/wallet/wallet.cpp#L2650" rel="noreferrer"><code>CreateTransaction</code></a> and later <a href="https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/wallet/wallet.cpp#L2397" rel="noreferrer"><code>SelectCoins</code></a> which are responsible for transaction creation and coin selection.</p>

<p>Some other things that are interesting in the wallet would include wallet creation and loading which occurs in <a href="https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/wallet/wallet.cpp#L3940" rel="noreferrer"><code>CreateWalletFromFile</code></a> and wallet encryption and unlocking which have good starting points of the <a href="https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/wallet/rpcwallet.cpp#L2087" rel="noreferrer"><code>encryptwallet</code> RPC</a> and <a href="https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/wallet/rpcwallet.cpp#L1902" rel="noreferrer"><code>walletpassphrase</code> RPC</a></p>

<h2>Startup and initialization</h2>

<p>Lastly some interesting occur during startup such as DNS seeding, connecting to nodes, loading the blockchain from disk, etc. The obvious and actual starting point is the <a href="https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/bitcoind.cpp#L193" rel="noreferrer"><code>main</code> function</a>. The main function is the entry point for pretty much every C/C++ program so it's a good place to look at for startup. Following the main function will bring you to <a href="https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/init.cpp#L1238" rel="noreferrer"><code>AppInitMain</code></a> which is where the bulk of loading and initialization occurs.</p>

CC BY-SA 4.0
2019-05-03T00:12:24.683Z
2019-05-03T00:12:24.683Z
Ava Chow
<p>Note that this is a general question and answer designed to serve as a guide for finding things in Bitcoin Core.</p>

<hr>

<p>Where in Bitcoin Core's source code does it do X? How can I find that code by myself?</p>

<hr>

<p>Example questions:</p>

<ul>
<li>Where does Bitcoin Core determine if a transaction is valid?</li>
<li>Where is the Proof of Work checked?</li>
<li>Where is the code for transaction creation?</li>
</ul>

<bitcoin-core><bitcoin-core-development>
Where in Bitcoin Core does it do X?
Ava Chow
# Where in Bitcoin Core does it do X?

(Asked by: username_0 on 2019-05-03)

Note that this is a general question and answer designed to serve as a guide for finding things in Bitcoin Core.

---

Where in Bitcoin Core's source code does it do X? How can I find that code by myself?

---

Example questions:

* Where does Bitcoin Core determine if a transaction is valid?
* Where is the Proof of Work checked?
* Where is the code for transaction creation?

**Tags:** bitcoin-core, bitcoin-core-development


---

## 1 answers

### Answer by username_0 on 2019-05-03. Score: 15 | Accepted answer

## Introduction

The key to finding out where a piece of code is without already knowing where it is is to start at the thing that will eventually lead to what you want to find. These can be logically thought through. For example, for relay and validation, these all occur after a node has received a block or transaction, so begin at the point where a block or transaction is received.

There are generally three kinds of actions that Bitcoin Core does: validation and relay of blocks and transactions, the wallet, and startup

## Validation and relay

In order for Bitcoin Core to be able to validate and relay a block or transaction, it has to first receive it. So logically the place to begin looking is at the point where a block or transaction has been received and it is beginning to be processed. That is in [`ProcessMessages` function in `src/net_processing.cpp`](https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/net_processing.cpp#L1774). Within this function, there are several `if` statements for each type of network message that can be received.

For transactions, you want to look at the `if` block for [`NetMsgType::TX`](https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/net_processing.cpp#L2393). By reading through the code in this `if` block and following the functions that are called, you will eventually reach where a transaction is verified, added to the mempool, and relayed. The most important function within this block is `AcceptToMemoryPool` and that is where all of the validation for an unconfirmed transaction is done.

For blocks, you want to look at the `if` block for [`NetMsgType::BLOCK`](https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/net_processing.cpp#L2773). Reading through this code will lead you to [`ProcessNewBlock`](https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L3559) and later [`ActivateBestChain`](https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L2651) and [`ConnectTip`](https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L2401) which are the functions that contain the validation of blocks.

## The wallet

Almost all of the wallet's functionality is centered around the creation and receiving of transactions. These logically begin when the user wants to send Bitcoin. So a good starting place is the [`sendtoaddress` RPC](https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/wallet/rpcwallet.cpp#L351). Following this function will take you to [`CreateTransaction`](https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/wallet/wallet.cpp#L2650) and later [`SelectCoins`](https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/wallet/wallet.cpp#L2397) which are responsible for transaction creation and coin selection.

Some other things that are interesting in the wallet would include wallet creation and loading which occurs in [`CreateWalletFromFile`](https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/wallet/wallet.cpp#L3940) and wallet encryption and unlocking which have good starting points of the [`encryptwallet` RPC](https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/wallet/rpcwallet.cpp#L2087) and [`walletpassphrase` RPC](https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/wallet/rpcwallet.cpp#L1902)

## Startup and initialization

Lastly some interesting occur during startup such as DNS seeding, connecting to nodes, loading the blockchain from disk, etc. The obvious and actual starting point is the [`main` function](https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/bitcoind.cpp#L193). The main function is the entry point for pretty much every C/C++ program so it's a good place to look at for startup. Following the main function will bring you to [`AppInitMain`](https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/init.cpp#L1238) which is where the bulk of loading and initialization occurs.


---

