Skip to content

Commit 6e15adb

Browse files
committed
jupyter: add base and tutorial
1 parent 4004c84 commit 6e15adb

File tree

4 files changed

+164
-30
lines changed

4 files changed

+164
-30
lines changed

01_overview-and-development.adoc

+72-14
Original file line numberDiff line numberDiff line change
@@ -776,33 +776,81 @@ Subsequent sections will contain various exercises related to their subject area
776776
To prepare for this we will begin with the following exercises which will ensure that our environment is ready:
777777

778778
. Build Bitcoin Core from source
779-
** Clone Bitcoin Core repository from GitHub
780-
** Check out the latest release tag (e.g. `v24.0.1`)
781-
** Install any dependencies required for your system
782-
** Follow the build instructions to compile the program
783-
** Run `make check` to run the unit tests
784-
** Follow the documentation to install dependencies required to run the functional tests
785-
*** Run the functional tests
786-
. Run a `bitcoind` node in regtest mode and control it using the `cli` tool
787-
** `./src/bitcoind -regtest` will start bitcoind in regtest mode. You can then control it using `./src/bitcoin-cli -regtest -getinfo`
779+
- [ ] Clone Bitcoin Core repository from GitHub
780+
- [ ] Check out the latest release tag (e.g. `v24.0.1`)
781+
- [ ] Install any dependencies required for your system
782+
- [ ] Follow the build instructions to compile the program
783+
- [ ] Run `make check` to run the unit tests
784+
- [ ] Follow the documentation to install dependencies required to run the functional tests
785+
- [ ] Run the functional tests
786+
. Run a `bitcoind` node in regtest mode and control it using the `cli` tool +
787+
+
788+
TIP: `./src/bitcoind -regtest` will start bitcoind in regtest mode. You can then control it using `./src/bitcoin-cli -regtest -getinfo`
788789
. Run and control a Bitcoin Core node using the `TestShell` python class from the test framework in a Jupyter notebook
789790
** See <<test_shell_nodes, Running nodes via Test Framework>> for more information on how to do this
791+
. Review a Pull Request from the repo
792+
- [ ] Find a PR (which can be open or closed) on GitHub which looks interesting and/or accessible
793+
- [ ] Checkout the PR locally
794+
- [ ] Review the changes
795+
- [ ] Record any questions that arise during code review
796+
- [ ] Build the PR
797+
- [ ] Test the PR
798+
- [ ] Break a test / add a new test
799+
- [ ] Leave review feedback on GitHub, possibly including:
800+
+
801+
ACK/NACK
802+
+
803+
Approach
804+
+
805+
How you reviewed it
806+
+
807+
Your system specifications if relevant
808+
+
809+
Any suggested nits
790810

791811
[#test_shell_nodes]
792812
.Running nodes via Test Framework
793813
****
814+
[discrete]
815+
== Why
816+
794817
Using Bitcoin Core's Test Framework means that nodes can be started, controlled and stopped using a python control class.
795-
Additionally, they are run in a temporary directory which is automatically removed by the system later, if not done
818+
Additionally, they are run in a temporary directory which is automatically removed by the operating system, if not done
796819
manually.
797820
798-
In addition to this, the `TestShell` class has an extremely similar interface to `bitcoin-cli`, where `bitcoin-cli` commands are mapped to `TestShell` methods, and arguments can be supplied positionally or as named values.
821+
In addition to this, the `TestShell` class has an extremely similar interface to `bitcoin-cli`, where most `bitcoin-cli` commands are mapped to `TestShell` methods, and arguments can be supplied positionally or as named values.
822+
Specifically, all RPCs are available to `TestShell`.
823+
However, certain `bitcoin-cli` commands, for example `-getinfo` require `bitcoin-cli` to call multiple RPCs and combine the results into something more user-friendly.
824+
These commands are not available to `TestShell`, but you can re-create them yourself using the source code!
825+
826+
When `TestShell` is combined with a jupyter notebook the result is easy-to-setup ephemeral nodes where iteration on complex commands is more pleasant than in the shell, and complex sequences of commands can be reproduced without having to write bash scripts or use shell history.
827+
828+
Once a complex command or sequence of commands is established, they can generally be translated to `bitcoin-cli` commands without much difficulty.
829+
830+
[discrete]
831+
== How
832+
833+
You **MUST** have a compiled `bitcoind` binary in the Bitcoin Core source directory.
834+
You can use any recent supported version of Bitcoin Core.
799835
800-
When combined with a jupyter notebook, the result is easy-to-setup ephemeral nodes where iteration on complex commands is more pleasant, and complex sequences of commands can be reproduced without having to write bash scripts or use shell history.
836+
In order to add startup (`bitcoind` program) options to our node(s) we need https://github.com/bitcoin/bitcoin/pull/26617/commits/989a52e0a50c0ae30a5c2bd3c08bb3ad1363a250[this^] commit.
837+
We can include this two ways:
801838
802-
To begin it is recommended to create a python virtual environment which can be done as follows when in the onboarding to bitcoin core top level directory:
839+
. Use the master branch of Bitcoin Core and running `git pull`, which will include the change.
840+
. Use any recent tag (e.g. v24.0.1) and running `git cherry-pick 989a52e0` to pull that change into the Test Framework code.
841+
842+
You **MUST** have a copy of the jupyter notebook, either manually downloaded from https://github.com/chaincodelabs/onboarding-to-bitcoin-core or by cloning the onboarding-to-bitcoin-core repo (recommended) with:
843+
844+
[source, bash]
845+
----
846+
git clone https://github.com/chaincodelabs/onboarding-to-bitcoin-core.git
847+
----
848+
849+
You **MAY** want to use a python virtual environment (recommended) which can be done as follows when in the onboarding to bitcoin core top level directory:
803850
804851
[source, bash]
805852
----
853+
cd /path/to/source/onboarding-to-bitcoin-core
806854
python3 -m venv "obc-venv"
807855
source obc-venv/bin/activate
808856
----
@@ -824,7 +872,7 @@ jupyter notebook
824872
----
825873
826874
This will open a list of all the files in this directory.
827-
Opening the file named `exercises.ipynb` will start the empty notebook containing instructions on how to use `TestShell` from the test Framework.
875+
Opening the file named `exercise_tutorial.ipynb` will start the notebook containing instructions on how to use `TestShell` from the test Framework.
828876
829877
When you are finished you can deactivate the venv using
830878
@@ -834,6 +882,16 @@ deactivate
834882
----
835883
836884
TIP: Don't forget to re-activate your venv each time you want to start the Jupyter notebook after deactivating the venv!
885+
886+
[discrete]
887+
== Quick use
888+
889+
Once you have familiarized yourself with the `TestShell` method using `exercise_tutorial.ipynb`, you can instead start new notebooks for exercises based on the `exercise_base.ipynb` notebook, which has much of the instruction removed and will let you get started faster.
890+
891+
If you correct the import path for your system in this file and save it, you can then easily make copies of it to use as start points for different exercises:
892+
893+
image::jupyter_duplicate.png[width=300]
894+
837895
****
838896

839897
////

exercise_base.ipynb

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 3,
6+
"metadata": {
7+
"pycharm": {
8+
"name": "#%%\n"
9+
}
10+
},
11+
"outputs": [],
12+
"source": [
13+
"import sys, os\n",
14+
"from test_framework.test_shell import TestShell\n",
15+
"\n",
16+
"sys.path.insert(0, os.path.expanduser(\"~/bitcoin/test/functional\"))\n",
17+
"test = TestShell().setup(\n",
18+
" num_nodes=2,\n",
19+
" setup_clean_chain=True,\n",
20+
" extra_args=[[], ['-fallbackfee=0.0002']],\n",
21+
")"
22+
]
23+
}
24+
],
25+
"metadata": {
26+
"kernelspec": {
27+
"display_name": "Python 3 (ipykernel)",
28+
"language": "python",
29+
"name": "python3"
30+
},
31+
"language_info": {
32+
"codemirror_mode": {
33+
"name": "ipython",
34+
"version": 3
35+
},
36+
"file_extension": ".py",
37+
"mimetype": "text/x-python",
38+
"name": "python",
39+
"nbconvert_exporter": "python",
40+
"pygments_lexer": "ipython3",
41+
"version": "3.9.7"
42+
}
43+
},
44+
"nbformat": 4,
45+
"nbformat_minor": 1
46+
}

exercises.ipynb renamed to exercise_tutorial.ipynb

+46-16
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010
"source": [
1111
"## Extending python path\n",
1212
"\n",
13-
"First we must add the path to Bitcoin Core's test_framework directory to the python system path\n",
13+
"First we must add Bitcoin Core's test_framework directory path to Python's system path.\n",
1414
"\n",
15-
"The test_framework is found within the Bitcoin Core source directory's `/test/functional` directory, e.g. as shown below\n",
16-
"`/home/will/src/bitcoin/test/functional`"
15+
"`test_framework` is found within Bitcoin Core's source directory, under `/test/functional`.\n",
16+
"Modify the value shown below to reflect the correct path on your system."
1717
]
1818
},
1919
{
2020
"cell_type": "code",
21-
"execution_count": 1,
21+
"execution_count": 3,
2222
"metadata": {},
2323
"outputs": [],
2424
"source": [
25-
"# Add the functional test framework to our PATH\n",
26-
"import sys\n",
27-
"sys.path.insert(0, \"/home/will/src/bitcoin/test/functional\")"
25+
"# Add the functional test framework to PATH\n",
26+
"import sys, os\n",
27+
"sys.path.insert(0, os.path.expanduser(\"~/bitcoin/test/functional\"))"
2828
]
2929
},
3030
{
@@ -43,7 +43,7 @@
4343
},
4444
{
4545
"cell_type": "code",
46-
"execution_count": 2,
46+
"execution_count": 4,
4747
"metadata": {
4848
"pycharm": {
4949
"name": "#%%\n"
@@ -65,12 +65,16 @@
6565
"source": [
6666
"## Node setup\n",
6767
"\n",
68-
"Finally, we must set up one or more nodes using the `setup()` method."
68+
"Finally, we must set up one or more nodes using the `setup()` method.\n",
69+
"\n",
70+
"We can pass `bitcoind` options to each node using the `extra_args` argument.\n",
71+
"This is the equivalent of setting options in the config file for `bitcoind` on the command line.\n",
72+
"Arguments are passed in as a list of lists, one list of arguments for each node."
6973
]
7074
},
7175
{
7276
"cell_type": "code",
73-
"execution_count": 3,
77+
"execution_count": 5,
7478
"metadata": {
7579
"pycharm": {
7680
"name": "#%%\n"
@@ -81,7 +85,7 @@
8185
"name": "stdout",
8286
"output_type": "stream",
8387
"text": [
84-
"2022-12-13T22:18:13.318000Z TestFramework (INFO): Initializing test directory /tmp/bitcoin_func_test_wbfjhnzg\n"
88+
"2022-12-14T13:28:53.132000Z TestFramework (INFO): Initializing test directory /tmp/bitcoin_func_test_r8jqr6ge\n"
8589
]
8690
}
8791
],
@@ -90,6 +94,7 @@
9094
"test = TestShell().setup(\n",
9195
" num_nodes=2,\n",
9296
" setup_clean_chain=True,\n",
97+
" extra_args=[[], ['-fallbackfee=0.0002']],\n",
9398
")"
9499
]
95100
},
@@ -105,22 +110,47 @@
105110
},
106111
{
107112
"cell_type": "code",
108-
"execution_count": null,
113+
"execution_count": 6,
109114
"metadata": {
110115
"pycharm": {
111116
"name": "#%%\n"
112117
}
113118
},
114119
"outputs": [],
115120
"source": [
116-
"node1 = test.nodes[0]\n",
117-
"node2 = test.nodes[1]\n"
121+
"test.nodes[0].getblockchaininfo()\n",
122+
"\n",
123+
"# or\n",
124+
"\n",
125+
"node2 = test.nodes[1]\n",
126+
"node2.getmemoryinfo()"
118127
]
119128
},
120129
{
121130
"cell_type": "markdown",
122131
"metadata": {},
123-
"source": []
132+
"source": [
133+
"## Shutdown\n",
134+
"\n",
135+
"When you are finished, you can shut down the nodes just you would via `bitcoin-cli` by using the `shutdown()` method as shown below.\n",
136+
"\n",
137+
"If you kill the jupyter notebook server without shutting down the nodes then they are usually torn down automatically, but there is a slim chance they remain operational.\n",
138+
"You can check for running `bitcoind` processes using `htop` or `sudo pidof bitcoind`."
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": 7,
144+
"metadata": {
145+
"pycharm": {
146+
"name": "#%%\n"
147+
}
148+
},
149+
"outputs": [],
150+
"source": [
151+
"test.nodes[0].stop()\n",
152+
"test.nodes[1].stop()"
153+
]
124154
}
125155
],
126156
"metadata": {
@@ -144,4 +174,4 @@
144174
},
145175
"nbformat": 4,
146176
"nbformat_minor": 1
147-
}
177+
}

images/jupyter_duplicate.png

33.7 KB
Loading

0 commit comments

Comments
 (0)