-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodebuilder
More file actions
executable file
·378 lines (334 loc) · 16.2 KB
/
nodebuilder
File metadata and controls
executable file
·378 lines (334 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/bin/bash
#
# A minimally-interactive script for launching a Bitcoin Core node
set -o errexit
set -o nounset
set -o pipefail
log_error() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S')] ERROR: $*" >&2
}
# shellcheck disable=SC2317
ensure_cursor_is_on_column_one() {
echo -en "\E[6n"
read -rsdR cursor_position
cursor_position="${cursor_position#*[}"
cursor_position_column="$(echo "${cursor_position}" | cut -d ";" -f 2)"
([ "${cursor_position_column}" -gt 1 ] && echo) || true
}
# shellcheck disable=SC2317
handle_exit() {
[ -n "${temp_directory}" ] && rm -rf -- "${temp_directory:?}"/
}
# shellcheck disable=SC2317
handle_error() {
ensure_cursor_is_on_column_one
log_error "Failed to run '${BASH_COMMAND}' on line $1."
}
trap handle_exit EXIT
trap 'handle_error ${LINENO:-}' ERR
target_bitcoin_version="26.0"
target_bitcoin_architecture="$(uname -m)"
temp_directory="$(mktemp -d)"
bitcoin_core_data_dir="${HOME}/.bitcoin"
user_data_dir="${XDG_DATA_HOME:-${HOME}/.local/share}"
shortcut_image_source="https://i.imgur.com/27V69CR.png"
shortcut_image_file="${user_data_dir}/images/bitcoin.png"
(timeout --preserve-status 0.1 nc -z 1.1.1.1 53 &&
timeout --preserve-status 0.1 nc -z 8.8.8.8 53 &&
timeout --preserve-status 0.5 nc -z bitcoincore.org 443) || {
err "ERROR: Check for active internet failed."
echo "Exiting."
exit 1
}
clear
sudo apt-get -qq update
echo -n "Performing a system upgrade... "
sudo NEEDRESTART_MODE=a apt-get -qq dist-upgrade --assume-yes
if [ -f /var/run/reboot-required ]; then
echo -en "\nREBOOT REQUIRED to upgrade the following:\n$(cat /var/run/reboot-required.pkgs)\n\nPRESS ANY KEY to reboot or press Ctrl+C to exit... "
read -rsn1 && echo
echo "Rebooting."
sudo reboot
exit 0
fi
echo "ok."
echo -n "Checking for dependencies..."
sudo apt-get -qq update && sudo apt-get -qq install --assume-yes --no-install-recommends curl git gnupg jq libxcb-xinerama0
echo "ok."
echo -n "Checking for Bitcoin ${target_bitcoin_version}... "
if command -v bitcoin-qt &> /dev/null; then
current_bitcoin_version="$(bitcoin-qt --version 2> /dev/null | head -1)"
else
current_bitcoin_version=""
fi
if [[ "${current_bitcoin_version}" == *"${target_bitcoin_version}"* ]]; then
echo "ok."
else
echo
bitcoin_core_extract_dir="${temp_directory}/bitcoin-core"
bitcoin_install_bin_source="${bitcoin_core_extract_dir}/bin"
bitcoin_install_include_source="${bitcoin_core_extract_dir}/include"
bitcoin_install_lib_source="${bitcoin_core_extract_dir}/lib"
bitcoin_install_man_source="${bitcoin_core_extract_dir}/share/man/man1"
bitcoin_install_bin_destination="/usr/local/bin"
bitcoin_install_include_destination="/usr/local/include"
bitcoin_install_lib_destination="/usr/local/lib"
bitcoin_install_man_destination="/usr/local/share/man/man1"
bitcoin_tarball_filename="bitcoin-${target_bitcoin_version}-${target_bitcoin_architecture}-linux-gnu.tar.gz"
bitcoin_tarball_destination_file="${HOME}/Downloads/${bitcoin_tarball_filename}"
if ! [ -f "${bitcoin_tarball_destination_file}" ]; then
bitcoin_source="https://bitcoincore.org/bin/bitcoin-core-${target_bitcoin_version}"
bitcoin_tarball_file_source="${bitcoin_source}/${bitcoin_tarball_filename}"
bitcoin_hash_filename="SHA256SUMS"
bitcoin_hash_file_source="${bitcoin_source}/${bitcoin_hash_filename}"
gpg_signatures_filename="SHA256SUMS.asc"
gpg_signatures_file_source="${bitcoin_source}/${gpg_signatures_filename}"
gpg_good_signatures_required="7"
guix_sigs_repo="https://github.com/bitcoin-core/guix.sigs.git"
bitcoin_tarball_temporary_file="${temp_directory}/${bitcoin_tarball_filename}"
bitcoin_hash_file="${temp_directory}/${bitcoin_hash_filename}"
gpg_signatures_file="${temp_directory}/${gpg_signatures_filename}"
guix_sigs_temporary_dir="${temp_directory}/guix.sigs"
guix_sigs_destination_dir="${HOME}/Downloads/guix.sigs"
echo -n " Downloading Bitcoin Core... "
curl --silent --show-error --location --fail --output "${bitcoin_tarball_temporary_file}" "${bitcoin_tarball_file_source}"
curl --silent --show-error --location --fail --output "${bitcoin_hash_file}" "${bitcoin_hash_file_source}"
curl --silent --show-error --location --fail --output "${gpg_signatures_file}" "${gpg_signatures_file_source}"
echo "ok."
echo -n " Validating the checksum... "
cd "${temp_directory}"/
sha256_check="$(grep "${bitcoin_tarball_filename}" "${bitcoin_hash_filename}" | sha256sum --check 2> /dev/null)"
cd "${HOME}"/
if ! [[ "${sha256_check}" == *"OK" ]]; then
err "INVALID. The download has failed. This script cannot continue due to security concerns."
err "Please review the temporary file ${temp_directory}/${bitcoin_hash_file}."
echo -en "\nPRESS ANY KEY to exit... "
read -rsn1 && echo
echo "Exiting."
exit 1
fi
echo "ok."
echo -n " Validating the signatures... "
if [ -d "${guix_sigs_destination_dir}"/ ]; then
gpg --quiet --import "${guix_sigs_destination_dir}"/builder-keys/*.gpg
else
git clone --single-branch --depth 1 --quiet "${guix_sigs_repo}" "${guix_sigs_temporary_dir}"
gpg --quiet --import "${guix_sigs_temporary_dir}"/builder-keys/*.gpg
fi
gpg_good_signature_count=$(gpg --verify "${gpg_signatures_file}" 2>&1 | grep -c "^gpg: Good signature from ")
if [[ "${gpg_good_signature_count}" -ge "${gpg_good_signatures_required}" ]]; then
echo "${gpg_good_signature_count} good."
[ -d "$(dirname "${bitcoin_tarball_destination_file}")" ] || mkdir -p "$(dirname "${bitcoin_tarball_destination_file}")"
mv "${bitcoin_tarball_temporary_file}" "${bitcoin_tarball_destination_file}"
[ -d "${guix_sigs_destination_dir}"/ ] || mv "${guix_sigs_temporary_dir}" "${guix_sigs_destination_dir}"
rm "${bitcoin_hash_file}"
rm "${gpg_signatures_file}"
else
err "INVALID. The download has failed. This script cannot continue due to security concerns."
err "Please review the temporary file ${temp_directory}/${gpg_signatures_file}."
echo -en "\nPRESS ANY KEY to exit... "
read -rsn1 && echo
echo "Exiting."
exit 1
fi
fi
echo -n " Extracting Bitcoin Core... "
[ -d "${bitcoin_core_extract_dir}"/ ] || mkdir "${bitcoin_core_extract_dir}"
tar -xzf "${bitcoin_tarball_destination_file}" -C "${bitcoin_core_extract_dir}"/ --strip-components=1
echo "ok."
echo -n " Running the unit tests... "
unit_test_response="$("${bitcoin_install_bin_source}"/test_bitcoin --log_level=nothing 2>&1)"
if [[ "${unit_test_response}" != *"No errors detected"* ]]; then
echo -e "\n${unit_test_response}"
log_error "Unit tests failed!"
echo "Exiting."
exit 1
fi
echo "ok."
echo -n " Installing Bitcoin Core... "
[ -d "${bitcoin_install_lib_destination}" ] || mkdir -p "${bitcoin_install_lib_destination}"
sudo cp "${bitcoin_install_lib_source}"/libbitcoinconsensus.so.0.0.0 "${bitcoin_install_lib_destination}"/libbitcoinconsensus.so.0.0.0
(cd /usr/local/lib && { sudo ln -s -f libbitcoinconsensus.so.0.0.0 libbitcoinconsensus.so.0 || { sudo rm -f libbitcoinconsensus.so.0 && sudo ln -s libbitcoinconsensus.so.0.0.0 libbitcoinconsensus.so.0; }; })
(cd /usr/local/lib && { sudo ln -s -f libbitcoinconsensus.so.0.0.0 libbitcoinconsensus.so || { sudo rm -f libbitcoinconsensus.so && sudo ln -s libbitcoinconsensus.so.0.0.0 libbitcoinconsensus.so; }; })
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/sbin" ldconfig -n "${bitcoin_install_lib_destination}"
[ -d "${bitcoin_install_bin_destination}" ] || mkdir -p "${bitcoin_install_bin_destination}"/
sudo install -c "${bitcoin_install_bin_source}"/{bitcoind,bitcoin-qt,bitcoin-cli,bitcoin-tx,bitcoin-util,bitcoin-wallet,test_bitcoin} "${bitcoin_install_bin_destination}"/
[ -d "${bitcoin_install_include_destination}" ] || mkdir -p "${bitcoin_install_include_destination}"/
sudo install -c -m 644 "${bitcoin_install_include_source}"/bitcoinconsensus.h "${bitcoin_install_include_destination}"/
[ -d "${bitcoin_install_man_destination}" ] || mkdir -p "${bitcoin_install_man_destination}"
sudo install -c -m 644 "${bitcoin_install_man_source}"/{bitcoind.1,bitcoin-qt.1,bitcoin-cli.1,bitcoin-tx.1,bitcoin-util.1,bitcoin-wallet.1} "${bitcoin_install_man_destination}"/
echo "ok."
echo -n " Removing temporary files... "
rm -r "${bitcoin_core_extract_dir:?}"/
rm -r "${temp_directory:?}"/
echo "ok."
fi
echo -n "Creating application shortcuts... "
desktop_path="${HOME}/Desktop"
applications_path="${user_data_dir}/applications"
shortcut_filename="bitcoin_core.desktop"
[ -d "$(dirname "${shortcut_image_file}")" ] || mkdir -p "$(dirname "${shortcut_image_file}")"
[ -f "${shortcut_image_file}" ] || curl --silent --show-error --location --fail --output "${shortcut_image_file}" "${shortcut_image_source}"
## Create .desktop on the user's Desktop and "Show Applications" directories
[ -d "${desktop_path}" ] || mkdir -p "${desktop_path}"
[ -d "${applications_path}" ] || mkdir -p "${applications_path}"
cat << EOF | tee "${applications_path}"/"${shortcut_filename}" > "${desktop_path}"/"${shortcut_filename}"
[Desktop Entry]
Name=Bitcoin Core
Comment=Launch Bitcoin Core
Exec=bitcoin-qt %u
Icon=${shortcut_image_file}
Terminal=false
StartupWMClass=Bitcoin Core
Type=Application
Categories=Application;
MimeType=application/bitcoin-paymentrequest;application/bitcoin-payment;application/bitcoin-paymentack;x-scheme-handler/bitcoin;
EOF
chmod u+x "${applications_path}"/"${shortcut_filename}"
chmod u+x "${desktop_path}"/"${shortcut_filename}"
## Make the desktop shortcut trusted
[ "${CI:-false}" = true ] || gio set "${desktop_path}"/"${shortcut_filename}" "metadata::trusted" true
echo "ok."
bitcoin_core_config_file="${bitcoin_core_data_dir}/bitcoin.conf"
if [ ! -f "${bitcoin_core_config_file}" ]; then
echo -n "Setting the default node behavior... "
[ -d "${bitcoin_core_data_dir}"/ ] || mkdir "${bitcoin_core_data_dir}"/
{
echo "server=1"
echo "mempoolfullrbf=1"
echo "v2transport=1"
} > "${bitcoin_core_config_file}"
echo "ok."
fi
if [ "$(grep --count --ignore-case "^prune=" "${bitcoin_core_config_file}")" == "0" ]; then
echo -n "Checking free space in home directory... "
free_space_in_mib=$(df --output=avail --block-size="1MiB" "${HOME}" | sed 1d)
echo "$((free_space_in_mib / 1024)) GiB."
bitcoin_core_blocks_dir="${bitcoin_core_data_dir}/blocks"
bitcoin_core_chainstate_dir="${bitcoin_core_data_dir}/chainstate"
echo -n "Checking data already synced... "
blocks_size_in_mib="0"
chainstate_size_in_mib="0"
if [ -d "${bitcoin_core_blocks_dir}"/ ]; then
blocks_size_in_mib=$(du -d0 --block-size="1MiB" "${bitcoin_core_blocks_dir}" | cut -f1)
fi
if [ -d "${bitcoin_core_chainstate_dir}"/ ]; then
chainstate_size_in_mib=$(du -d0 --block-size="1MiB" "${bitcoin_core_chainstate_dir}" | cut -f1)
fi
data_already_synced_in_mib=$((blocks_size_in_mib + chainstate_size_in_mib))
echo "$((data_already_synced_in_mib / 1024)) GiB."
## This constant will need to be adjusted over time as the chain grows
## or need to find how to generate this dynamically in a trustless way.
## The lower this number is, the more likely disk space errors during IBD.
## The higher this number is, the more likely a node will be to prune.
## The sweet spot is about 50 to 100 GB more than the size of blocks/ + chainstate/,
## which, as of June 2023, is around 522 GiB.
archival_node_required_disk_in_gib="600"
archival_node_required_disk_in_mib=$((archival_node_required_disk_in_gib * 1024))
if [ "${free_space_in_mib}" -ge $((archival_node_required_disk_in_mib - data_already_synced_in_mib)) ]; then
echo " Your node will run as an unpruned full node."
echo "prune=0" >> "${bitcoin_core_config_file}"
elif [ "${free_space_in_mib}" -lt $((archival_node_required_disk_in_mib / 80 - data_already_synced_in_mib)) ]; then
err "You are too low on disk space to run Bitcoin Core."
echo "Exiting."
exit 1
else
if [ "${free_space_in_mib}" -lt $((archival_node_required_disk_in_mib / 40 - data_already_synced_in_mib)) ]; then
echo -e " Your disk space is low.\n Setting blocks-only mode and the minimum 0.55 GiB prune."
echo "blocksonly=1" >> "${bitcoin_core_config_file}"
prune_amount_in_mib="550"
else
if [ "${free_space_in_mib}" -lt $((archival_node_required_disk_in_mib / 12 - data_already_synced_in_mib)) ]; then
prune_ratio=20
elif [ "${free_space_in_mib}" -lt $((archival_node_required_disk_in_mib / 4 - data_already_synced_in_mib)) ]; then
prune_ratio=40
elif [ "${free_space_in_mib}" -lt $((archival_node_required_disk_in_mib * 3 / 4 - data_already_synced_in_mib)) ]; then
prune_ratio=60
else
prune_ratio=80
fi
prune_amount_in_mib=$((free_space_in_mib * prune_ratio / 100))
echo -e " Pruning to $((prune_amount_in_mib / 1024)) GiB (${prune_ratio}% of the free space).\n You can change this in ~/.bitcoin/bitcoin.conf."
fi
echo "prune=${prune_amount_in_mib}" >> "${bitcoin_core_config_file}"
fi
fi
echo -n "Starting Bitcoin Core... "
if [ "${CI:-false}" = true ]; then
bitcoind --daemonwait
else
bitcoin-qt 2> /dev/null &
disown
fi
echo "ok."
echo -n " Checking the RPC status... "
bitcoin-cli --rpcwait getrpcinfo > /dev/null
echo "ok."
sleep_time="10"
blockchain_info=$(bitcoin-cli --rpcwait getblockchaininfo)
ibd_status=$(jq '.initialblockdownload' <<< "${blockchain_info}")
if [ "${ibd_status}" = true ]; then
echo -n "Disabling system sleep, suspend, and hibernate... "
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target &> /dev/null
echo "ok."
echo -en "\nClose this Terminal window by clicking on the \"X\".\nThis screen will refresh in ${sleep_time} seconds."
for ((i = 1; i <= sleep_time; i++)); do
sleep 1
printf "."
done
echo
else
echo " Your node has already synced the blockchain."
fi
while [ "${ibd_status}" = true ]; do
sync_progress=$(jq '.verificationprogress' <<< "${blockchain_info}")
# Handle case of early sync by replacing scientific notation with decimal
[[ "${sync_progress}" == *"e"* ]] && sync_progress="0.000000001"
sync_progress_percent=$(echo "${sync_progress}*100" | bc)
blocks=$(jq '.blocks' <<< "${blockchain_info}")
headers=$(jq '.headers' <<< "${blockchain_info}")
last_block_time=$(jq '.time' <<< "${blockchain_info}")
size_on_disk_in_mib=$(($(jq '.size_on_disk' <<< "${blockchain_info}") / 1024 / 1024))
free_space_in_mib=$(df --output=avail --block-size="1MiB" "${bitcoin_core_data_dir}" | sed 1d)
sync_status=""
if [ "${headers}" -eq 0 ]; then
sync_status="${sync_status}Pre-syncing the block headers (first pass).\n"
elif [ "${blocks}" -eq 0 ]; then
sync_status="${sync_status}Syncing the block headers (second pass).\n"
sync_status="${sync_status}Headers synced: ${headers}"
else
sync_status="${sync_status}Sync progress: $(printf '%.3f' "${sync_progress_percent}") %\n"
sync_status="${sync_status}Blocks remaining: $((headers - blocks))\n"
sync_status="${sync_status}Current chain tip: $(date -d @"${last_block_time}" | cut -c 5-)\n"
fi
sync_status="${sync_status}\nChain sync size: "
if [ "${size_on_disk_in_mib}" -gt 1024 ]; then
sync_status="${sync_status}$((size_on_disk_in_mib / 1024)) GiB\n"
else
sync_status="${sync_status}${size_on_disk_in_mib} MiB\n"
fi
sync_status="${sync_status}Disk free space: "
if [ "${free_space_in_mib}" -gt 1024 ]; then
sync_status="${sync_status}$((free_space_in_mib / 1024)) GiB\n"
else
sync_status="${sync_status}${free_space_in_mib} MiB\n"
fi
clear
echo -en "${sync_status}\n"
if [ "${CI:-false}" = true ] && [ "${size_on_disk_in_mib}" -ge 1 ]; then
echo "Exiting the script for CI runners."
bitcoin_pid="$(cat "${bitcoin_core_data_dir}"/bitcoind.pid)"
bitcoin-cli stop
while ps -p "${bitcoin_pid}"; do sleep 0.01; done &> /dev/null
exit 0
fi
echo -e "Synchronizing can take weeks on a slow connection.\n"
echo -e "Close this Terminal window by clicking on the \"X\"."
echo -n "This screen will refresh in ${sleep_time} seconds."
sleep "${sleep_time}"
blockchain_info=$(bitcoin-cli --rpcwait getblockchaininfo)
echo
ibd_status=$(jq '.initialblockdownload' <<< "${blockchain_info}")
done
echo "This script has completed successfully."
exit 0