-
-
Notifications
You must be signed in to change notification settings - Fork 805
Create first draft of Chapter 15: Setting up a Bitcoin Regtest #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hi @ChristopherA, I am willing to go as far as you want on this topic. I covered the basics of Regtest startup, mining and testing the created blockchain. |
I also suggest switching orders of 15.2 and 15.3. To test, first you'll have to mine the blocks and generate some output. |
@shannona You OK with switching 15.2 and 15.3? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, this looks like a great start. Thanks so much for taking this on! My main comment is that I'd like to see more results. That's been a very important part of the tutorials: (1) show the commands; (2) explain the commands; (3) shows the results; (4) explain the results. For your new chapter here, the main thing that can be added are the results: not just what the command output, but also what other things we can see now that it's output, such as transactions and blocks. With that in hand, we can deliver a very thorough explanation of how this all works.
15_1_Building_the_Regtest.md
Outdated
|
||
To start your Bitcoind (Bitcoin Daemon) in Regtest mode and create a private Blockchain, you have to use the following command: | ||
``` | ||
$ bitcoind -regtest -daemon |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see some comments on how this interrelates with a normal setup, which users will presumably have from other parts of the tutorial. Should they start from scratch with a newly create VM? Can they run this in parallel with their existing setups? Can they run it parallel, but not simultaneously?
15_1_Building_the_Regtest.md
Outdated
|
||
Regtest wallets and block chain state (chainstate) are saved in the regtest subdirectory of the Bitcoin configuration directory. | ||
If you want to start a brand new Blockchain using the Regtest mode, all you have to do is delete the `regtest` folder and restart the Bitcoind. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always great to show the command line examples, so here it might be nice to see what that subdirectory looks like. I think I did the same thing in the early sections.
15_2_Testing_with_Regtest.md
Outdated
After [mining blocks](15_3_Mining_with_Regtest.md) and getting the rewards, you can verify the balance on your wallet: | ||
``` | ||
$ bitcoin-cli -regtest getbalance | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'd like to show the results too.
15_2_Testing_with_Regtest.md
Outdated
For example, to create a transaction and include into a block: | ||
``` | ||
$ bitcoin-cli -regtest sendtoaddress [address] 10.0 | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Results.
15_2_Testing_with_Regtest.md
Outdated
``` | ||
|
||
And after it, if your application requires 6 confirmations, you can mine additional 6 blocks into your Regtest chain: | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice to have a little bit additional explanation on this, though that might prove unnecessary once you flip the chapters around.
15_2_Testing_with_Regtest.md
Outdated
And after it, if your application requires 6 confirmations, you can mine additional 6 blocks into your Regtest chain: | ||
``` | ||
$ bitcoin-cli -regtest generate 6 | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Results?
If that command doesn't give results, maybe show the transaction with the confirmations.
15_3_Mining_with_Regtest.md
Outdated
You can generate/mine new blocks using the RPC method `generate`. This method is only available in the Regtest mode, using the following command: | ||
``` | ||
$ bitcoin-cli -regtest generate 101 | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Results? Or otherwise show that we now have 101 blocks.
15_3_Mining_with_Regtest.md
Outdated
``` | ||
|
||
This command will generate 101 blocks using a special RPC which is only available in regtest mode. This takes less than a second on a generic PC. | ||
Because this is a new block chain using Bitcoin’s default rules, the first blocks pay a block reward of 50 bitcoins. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice to show how that reward shows up too.
@brunocvcunha, definitely feel free to flip the chapters around. My experience in writing this was that no chapter outline ever survived first contact with the writing. It's just meant to be a starting position, so expansions or contractions of topics or changing of ordering are all perfectly fine. (Looking at your sections, that's exactly what I would have done.) |
Got it! Thanks for the detailed review. I'll check all of your points and push the changes later today. |
@kallewoof had suggested that in the regtest section the testing tool https://www.npmjs.com/package/bitcointest might be useful. Thoughts? |
I think it's an interesting topic to cover, allowing you to simulate edge cases that might happen in the real world. Maybe show the configuration, an example, and link to their repo at https://github.com/dgarage/bitcointest? |
@shannona I improved & reviewed the chapter considering your reviews. Let me know if something is still off. |
Added some details about the bitcointest: https://github.com/ChristopherA/Learning-Bitcoin-from-the-Command-Line/pull/15/files#diff-6ce8301eb7b94e92c2df2c93d324daccR81 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally some technicalities, and general use of "the Regtest [Mode]" which I find unusual and recommend changing.
15_3_Testing_with_Regtest.md
Outdated
``` | ||
```javascript | ||
const { BitcoinNet, BitcoinGraph } = require('bitcointest'); | ||
net = new BitcoinNet('/usr/local/bin', '/tmp/bitcointest/', 22001, 22002); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not const net
and const graph
here? They don't seem to be modified and if they were, it should be let net
/ let graph
.
15_1_Building_the_Regtest.md
Outdated
@@ -0,0 +1,31 @@ | |||
# 15.1: Building the Regtest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what this title is insinuating. You don't build regtest, and the use of the
is weird.
15_0_Setting_Up_a_Bitcoin_Regtest.md
Outdated
|
||
## Table of Contents | ||
|
||
* [Section One: Building the Regtest](15_1_Building_the_Regtest.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See other comment -- I think this title needs to be changed.
15_1_Building_the_Regtest.md
Outdated
If you want to start a brand new Blockchain using the Regtest mode, all you have to do is delete the `regtest` folder and restart the Bitcoind: | ||
``` | ||
$ rm -rf regtest | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically you would just make a new datadir and use the -datadir
option.
E.g.
$ mkdir d
$ bitcoind -regtest -datadir=d
$ alias bcli="bitcoin-cli -regtest -datadir=d"
$ bcli [...]
15_1_Building_the_Regtest.md
Outdated
|
||
## What's Next? | ||
|
||
After starting your bitcoind in the Regtest mode, you can now use Regtest-specific RPC commands to [generate/mine blocks in your private chain](15_2_Mining_with_Regtest.md). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consistent use of the Regtest [Mode]
-- I suggest nixing the
-- i.e. running bitcoind on regtest
15_2_Mining_with_Regtest.md
Outdated
> **NOTE:** This is a draft in progress, so that I can get some feedback from early reviewers. It is not yet ready for learning. | ||
|
||
This document explains how to generate (i.e. mine) blocks using a Regtest (Regression Test) blockchain. | ||
To generate the Genesis block and the next blocks on a new blockchain requires very minimal proof-of-work, due to the low difficulty and that it follows the Testnet rules. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The genesis block is not generated, it's hard coded into bitcoin for all networks (main, test, and regtest).
15_2_Mining_with_Regtest.md
Outdated
|
||
## Generating blocks | ||
|
||
You can generate/mine new blocks using the RPC method `generate`. This method is only available in the Regtest mode, using the following command: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generate
is technically available in all modes (I would call them networks not modes, actually), but it's not very likely to yield any blocks on mainnet or testnet due to the high difficulty. (Anecdote: I tried for a couple months awhile back. Alas, my balance remained 0)
15_2_Mining_with_Regtest.md
Outdated
The output is the block hash of every block generated (in our example, 101 hashes). | ||
|
||
|
||
This command will generate 101 blocks using a special RPC which is only available in regtest mode. This takes less than a second on a generic PC. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above comment.
15_2_Mining_with_Regtest.md
Outdated
|
||
This command will generate 101 blocks using a special RPC which is only available in regtest mode. This takes less than a second on a generic PC. | ||
Because this is a new block chain using Bitcoin’s default rules, the first blocks pay a block reward of 50 bitcoins. | ||
Unlike mainnet, in regtest mode only the first 150 blocks pay a reward of 50 bitcoins. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should mention that the subsidy halving period for regtest is 150 blocks, so after 150 blocks you receive half of the prior amount (i.e. 25), and after another 150 (=300) you receive 12.5, and so on. It looks right now as if you stop receiving reward after 150 blocks.
15_2_Mining_with_Regtest.md
Outdated
|
||
## What's Next? | ||
|
||
After starting your bitcoind in the Regtest mode and generating the first blocks, you have balance in your address to spend and [test the Regtest blockchain](15_3_Testing_with_Regtest.md). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After starting bitcoind on regtest and
[...]
Thanks for your input! I think I was really overusing the word "mode", but "Regtest mode" is a common expression to describe the Bitcoin Core started with How does it look now? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to accept this pull request as is, but we probably need to come up with some header for chapters that talks about their level of review and completeness. For instance, this version is sufficiently informative, but I think still needs to be fitted into the overall pedagogy not only of "What is regtest" but also "why regtest", "what it is regtest actually good for", and "how learning regtest help you understand X".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Several cases of the Regtest
or the Mainnet
, uppercase Blockchain
, use of private
instead of local
, and some other issues. This PR was merged so maybe make a new PR to address this?
@@ -0,0 +1,18 @@ | |||
# Chapter 15: Setting Up a Bitcoin Regtest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove a
in a Bitcoin Regtest
.
@@ -0,0 +1,18 @@ | |||
# Chapter 15: Setting Up a Bitcoin Regtest | |||
|
|||
While developing Bitcoin applications, you might want to use your applications isolated from any public Blockchain such as the Mainnet or the Testnet. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the
from Mainnet
/ Testnet
and don't capitalize. (such as mainnet or testnet
). This applies to the remainder of this PR (i.e. below 'the Regtest', etc).
# Chapter 15: Setting Up a Bitcoin Regtest | ||
|
||
While developing Bitcoin applications, you might want to use your applications isolated from any public Blockchain such as the Mainnet or the Testnet. | ||
You can create a Blockchain from scratch using the Regtest, with one main advantage over Testnet: you choose when to create new blocks, so you have complete control over the environment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decapitalize Blockchain
(-> blockchain
). This applies to the remainder of this PR.
|
||
## Table of Contents | ||
|
||
* [Section One: Starting the Regtest](15_1_Starting_the_Regtest.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This name is confusing and misleading. I suggest Starting a Node on the Regtest Network
|
||
> **NOTE:** This is a draft in progress, so that I can get some feedback from early reviewers. It is not yet ready for learning. | ||
|
||
This document explains how to build a Regtest (Regression Test) by hand to be able to develop new applications without the need to interact with other peers and blocks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how to set up a bitcoin node locally using the regtest (regression test) network in order to develop new applications
[...]
|
||
## Generating blocks | ||
|
||
You can generate/mine new blocks using the RPC method `generate`. It only makes sense to use this method on regtest, due to the high difficulty it's very unlikely that it will yield to new blocks in the mainnet or testnet: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that it will yield any new blocks on mainnet or testnet
The output is the block hash of every block generated (in our example, 101 hashes). | ||
|
||
|
||
This command will generate 101 blocks using a special RPC to generate the blocks on your regtest network. Running this command only makes sense on the regtest, if you try to run on the mainnet or testnet, it is very unlikely that it will be able to yield any block. On regtest, this takes less than a second on a generic PC. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First sentence is awkward. This paragraph repeats what is written above mostly.
|
||
|
||
This command will generate 101 blocks using a special RPC to generate the blocks on your regtest network. Running this command only makes sense on the regtest, if you try to run on the mainnet or testnet, it is very unlikely that it will be able to yield any block. On regtest, this takes less than a second on a generic PC. | ||
Because this is a new block chain using Bitcoin’s default rules, the first blocks pay a block reward of 50 bitcoins. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blockchain
(one word)
## Validating the Regtest | ||
Now you should be able to use this balance for any type of interaction with the private Blockchain, such as sending Bitcoin transactions according to [Chapter 4]((04_0_Sending_Bitcoin_Transactions.md)) in this guide. The only difference is that you need to use the flag `-regtest` when running the `bitcoin-cli` in order for the request to be sent to the Regtest Bitcoin daemon. | ||
|
||
It is important to note that for your transactions to complete, you will have to generate/mine new blocks so that the transactions can be included into them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use for your transactions to confirm
here -- I think the user should know what that means at this point (Chapter 15).
## Testing with Regtest | ||
|
||
When you are on regtest, you are able to simulate edge cases and attacks that might happen in the real world, such as Double Spend. | ||
We are going to use the package [bitcointest by dgarage](https://github.com/dgarage/bitcointest) to simulate a transaction from one wallet to another, but you can check [their guide](https://www.npmjs.com/package/bitcointest) for more specific attack simulations, such as Double Spend. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bitcointest by Digital Garage
This is a draft of Chapter 15, will be looking into improving and completing it