<p>I'm not sure about "learning the codebase", but I can point you to some files which are in my opinion good starting points for explorations:</p>

<ul>
<li>The <a href="https://github.com/bitcoin/bitcoin/blob/master/src/init.cpp" rel="nofollow noreferrer">init.cpp</a>: "what happens when I start my bitcoind ?"</li>
<li>The rpc calls in <a href="https://github.com/bitcoin/bitcoin/tree/master/src/rpc" rel="nofollow noreferrer">src/rpc</a> or <a href="https://github.com/bitcoin/bitcoin/blob/master/src/wallet/rpcwallet.cpp" rel="nofollow noreferrer">src/wallet/rpcwallet</a>.<br>
For example, "what happens when I <a href="https://github.com/bitcoin/bitcoin/blob/8830cb58abc888144a1edb9b2fba427716cc45d8/src/wallet/rpcwallet.cpp#L357" rel="nofollow noreferrer"><code>sendtoaddress</code></a> ?"</li>
</ul>

<p>In addition there are also other parts that might be less good starting point, but nonetheless very interesting. Such as the <a href="https://github.com/bitcoin/bitcoin/blob/8830cb58abc888144a1edb9b2fba427716cc45d8/src/script/interpreter.cpp#L281" rel="nofollow noreferrer">script interpreter</a> or the <a href="https://github.com/bitcoin/bitcoin/blob/8830cb58abc888144a1edb9b2fba427716cc45d8/src/net_processing.cpp#L1883" rel="nofollow noreferrer">network messages process</a>.</p>

CC BY-SA 4.0
2019-12-30T15:23:41.617Z
Overall that's non exhaustive and opinionated. There are people regularly answering here who know the codebase much more than I do, so there might be other great starting points that I didn't mention.
Antoine Poinsot
CC BY-SA 4.0
2019-12-30T15:21:27.750Z
2019-12-30T15:21:27.750Z
Antoine Poinsot
<p>Some good places to start are the places that the user or the network input something into Bitcoin Core. The obvious two places for these are the RPC functions and the network message processing. The RPC code is in <code>src/rpc</code>, <code>src/wallet/rpcwallet.cpp</code> and <code>src/rpcdump.cpp</code>. The network message processing is in <code>src/net_processing.cpp</code>. By tracing through the function calls and also looking up where the global variables are being added to and set, you can get into most parts of the codebase.</p>

<p>For example, suppose you looked at <code>sendtoaddress</code>. You would start at the RPC function in <code>src/wallet/rpcwallet.cpp</code> and trace the execution path. You would see that it eventually goes to <code>src/wallet/wallet.cpp</code> and how it uses the <code>CreateTransaction</code> function. From here, you would learn how coin selection works.</p>

<p>Now you would see how the wallet chooses coins for transaction spending. But how does it find out about those in the first place? Well, as you are tracing <code>sendtoaddress</code>, you will come across the <code>AvailableCoins</code> which iterates through some variable named <code>mapWallet</code> and pulls from stuff in <code>mapWallet</code> to build a list of selectable coins. Thus <code>mapWallet</code> must be where transactions are stored, so you can search for the variable <code>mapWallet</code> in the codebase. This will tell you where that variable is being used and you can trace through the uses of <code>mapWallet</code> to learn how it is populated and used.</p>

<p>Doing so will lead you to the functions that populate <code>mapWallet</code>. So now you want to trace backwards: what are calling those functions, how did we get to that function. For example, for populating <code>mapWallet</code>, you will run into <code>AddToWallet</code>. What calls <code>AddToWallet</code>? The function <code>AddToWalletIfInvolvingMe</code>. And you can keep going backwards to get an idea of how it all works.</p>

<p>The same methodology can be applied to the network message processing. For example, suppose you wanted to learn about block processing. The place to start is when a block is received, so in <code>src/net_processing.cpp</code>, look for <code>NetMsgType::BLOCK</code> within the <code>ProcessMessage</code> function. From here you can trace the function calls and global variables like we did with the RPCs. It will take you to the Proof of Work validation and how the target is calculated for a block. It will take you to transaction validation. You will end up looking at the script interpreter and how it evaluates scripts in a transaction. And so on.</p>

<p>You can repeat this for every RPC and every network message type to find out where and how things are processed. This will get you through the majority of the codebase. Network message processing will get you through all of consensus and the bulk of the codebase. Tracing RPCs will get you through basically everything else because almost everything a user can do can be done through the RPCs. These two general entry points are great places to go through the codebase and learn about what Bitcoin  Core is doing internally.</p>

CC BY-SA 4.0
2019-12-30T17:21:18.683Z
2019-12-30T17:21:18.683Z
Ava Chow
<p>I've read Mastering Bitcoin and other high level resources and I believe I have a decent understanding of Bitcoin. I'd now like to read and understand the source code. However, I'm very new to cpp and don't know a good way to approach this task. Is there a systematic approach I can use to effectively learn the codebase?</p>

<source-code>
What is a good approach to learning the Bitcoin codebase?
Matthew Cruz
# What is a good approach to learning the Bitcoin codebase?

(Asked by: username_0 on 2019-12-29)

I've read Mastering Bitcoin and other high level resources and I believe I have a decent understanding of Bitcoin. I'd now like to read and understand the source code. However, I'm very new to cpp and don't know a good way to approach this task. Is there a systematic approach I can use to effectively learn the codebase?

**Tags:** source-code


---

## 2 answers

### Answer by username_2 on 2019-12-30. Score: 4 | Accepted answer

Some good places to start are the places that the user or the network input something into Bitcoin Core. The obvious two places for these are the RPC functions and the network message processing. The RPC code is in `src/rpc`, `src/wallet/rpcwallet.cpp` and `src/rpcdump.cpp`. The network message processing is in `src/net_processing.cpp`. By tracing through the function calls and also looking up where the global variables are being added to and set, you can get into most parts of the codebase.

For example, suppose you looked at `sendtoaddress`. You would start at the RPC function in `src/wallet/rpcwallet.cpp` and trace the execution path. You would see that it eventually goes to `src/wallet/wallet.cpp` and how it uses the `CreateTransaction` function. From here, you would learn how coin selection works.

Now you would see how the wallet chooses coins for transaction spending. But how does it find out about those in the first place? Well, as you are tracing `sendtoaddress`, you will come across the `AvailableCoins` which iterates through some variable named `mapWallet` and pulls from stuff in `mapWallet` to build a list of selectable coins. Thus `mapWallet` must be where transactions are stored, so you can search for the variable `mapWallet` in the codebase. This will tell you where that variable is being used and you can trace through the uses of `mapWallet` to learn how it is populated and used.

Doing so will lead you to the functions that populate `mapWallet`. So now you want to trace backwards: what are calling those functions, how did we get to that function. For example, for populating `mapWallet`, you will run into `AddToWallet`. What calls `AddToWallet`? The function `AddToWalletIfInvolvingMe`. And you can keep going backwards to get an idea of how it all works.

The same methodology can be applied to the network message processing. For example, suppose you wanted to learn about block processing. The place to start is when a block is received, so in `src/net_processing.cpp`, look for `NetMsgType::BLOCK` within the `ProcessMessage` function. From here you can trace the function calls and global variables like we did with the RPCs. It will take you to the Proof of Work validation and how the target is calculated for a block. It will take you to transaction validation. You will end up looking at the script interpreter and how it evaluates scripts in a transaction. And so on.

You can repeat this for every RPC and every network message type to find out where and how things are processed. This will get you through the majority of the codebase. Network message processing will get you through all of consensus and the bulk of the codebase. Tracing RPCs will get you through basically everything else because almost everything a user can do can be done through the RPCs. These two general entry points are great places to go through the codebase and learn about what Bitcoin Core is doing internally.


---

### Answer by username_1 on 2019-12-30. Score: 2

I'm not sure about "learning the codebase", but I can point you to some files which are in my opinion good starting points for explorations:

* The [init.cpp](https://github.com/bitcoin/bitcoin/blob/master/src/init.cpp): "what happens when I start my bitcoind ?"
* The rpc calls in [src/rpc](https://github.com/bitcoin/bitcoin/tree/master/src/rpc) or [src/wallet/rpcwallet](https://github.com/bitcoin/bitcoin/blob/master/src/wallet/rpcwallet.cpp).  
  For example, "what happens when I [`sendtoaddress`](https://github.com/bitcoin/bitcoin/blob/8830cb58abc888144a1edb9b2fba427716cc45d8/src/wallet/rpcwallet.cpp#L357) ?"

In addition there are also other parts that might be less good starting point, but nonetheless very interesting. Such as the [script interpreter](https://github.com/bitcoin/bitcoin/blob/8830cb58abc888144a1edb9b2fba427716cc45d8/src/script/interpreter.cpp#L281) or the [network messages process](https://github.com/bitcoin/bitcoin/blob/8830cb58abc888144a1edb9b2fba427716cc45d8/src/net_processing.cpp#L1883).

> **Comments:**
> **username_1**: Overall that's non exhaustive and opinionated. There are people regularly answering here who know the codebase much more than I do, so there might be other great starting points that I didn't mention.  

---

