Skip to content

Conversation

@Empact
Copy link
Contributor

@Empact Empact commented Jun 8, 2022

This reworks/revives #15848 to add a check for low disk space on first startup and issue a warning if disk space is below the expected space required to accommodate the blocks.

This PR was fashioned by a team of developers at the bitcoin++ conference workshop: "Let's contribute to Bitcoin Core"

Fixes #15813

@Empact Empact force-pushed the disk-space-check branch 3 times, most recently from 3ec10dd to 914e1e8 Compare June 8, 2022 22:40
@litch
Copy link
Contributor

litch commented Jun 9, 2022

Confirmed this in an underpowered VM. I know it was discussed to use a warning instead of an error. I think that a warning, however, should probably not terminate the process. So perhaps it would be better to change this function to return true after emitting the warning.

2022-06-09T11:42:03Z init message: Verifying blocks…
2022-06-09T11:42:03Z  block index             463ms
2022-06-09T11:42:03Z Warning: Disk space for "/home/litch/.bitcoin/blocks" may not accommodate the block files
Warning: Disk space for "/home/litch/.bitcoin/blocks" may not accommodate the block files
2022-06-09T11:42:03Z Shutdown: In progress...
2022-06-09T11:42:03Z scheduler thread exit
2022-06-09T11:42:03Z Shutdown: done
litch@bitcoind-test:~/bitcoin$

@Empact Empact force-pushed the disk-space-check branch from 914e1e8 to 86bb6f4 Compare June 9, 2022 13:55
@Empact
Copy link
Contributor Author

Empact commented Jun 9, 2022

So perhaps it would be better to change this function to return true after emitting the warning.

Well thanks for testing - in this case we don't need to return at all, as the intention is to note the issue and carry on. Removed the return statement.

@Empact Empact force-pushed the disk-space-check branch from 86bb6f4 to 5fa1f76 Compare June 9, 2022 13:59
@Empact Empact force-pushed the disk-space-check branch 4 times, most recently from 735f677 to f2d7bf5 Compare June 9, 2022 15:31
@litch
Copy link
Contributor

litch commented Jun 9, 2022

...
2022-06-09T15:43:48Z Initializing databases...
2022-06-09T15:43:48Z Opening LevelDB in /home/litch/.bitcoin/chainstate
2022-06-09T15:43:49Z Opened LevelDB successfully
2022-06-09T15:43:49Z Wrote new obfuscate key for /home/litch/.bitcoin/chainstate: 5da37d724c848cd8
2022-06-09T15:43:49Z Using obfuscation key for /home/litch/.bitcoin/chainstate: 5da37d724c848cd8
2022-06-09T15:43:49Z init message: Verifying blocks…
2022-06-09T15:43:49Z  block index            1014ms
2022-06-09T15:43:49Z Warning: Disk space for "/home/litch/.bitcoin/blocks" may not accommodate the block files. Approximately 460 GB of data will be stored in this directory.
Warning: Disk space for "/home/litch/.bitcoin/blocks" may not accommodate the block files. Approximately 460 GB of data will be stored in this directory.
2022-06-09T15:43:49Z loadblk thread start
2022-06-09T15:43:49Z UpdateTip: new best=000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f height=0 version=0x00000001 log2_work=32.000022 tx=1 date='2009-01-03T18:15:05Z' progr
ess=0.000000 cache=0.0MiB(0txo)
2022-06-09T15:43:49Z Failed to open mempool file from disk. Continuing anyway.
2022-06-09T15:43:49Z loadblk thread exit
2022-06-09T15:43:49Z block tree size = 1
...

When running on a VM with a small HDD, this prints the warning as expected.

After downloading a number of blocks, then re-starting bitcoind, the warning is not re-emitted.

@Empact Empact force-pushed the disk-space-check branch 2 times, most recently from ce0c198 to 05ff20c Compare June 16, 2022 05:42
@Empact
Copy link
Contributor Author

Empact commented Jun 16, 2022

Disabled the warning on fReindexChainState, and reworked it to avoid duplicative check in the cases where additional_bytes_needed would be 0.

@Empact Empact force-pushed the disk-space-check branch 2 times, most recently from 11c10ef to 6f935fd Compare June 16, 2022 05:57
@Empact
Copy link
Contributor Author

Empact commented Jun 16, 2022

Moved the check further down initialization, to the point where chain_active_height was already being read, so we don't need to do any new locking - a question to test is: will the height be the appropriate value at this time in the startup?

@luke-jr
Copy link
Member

luke-jr commented Jun 18, 2022

Concept ACK (as a warning only).

@achow101
Copy link
Member

I don't think the current location in initialization is correct. ~50 lines above, we start the thread for importing blocks from an external file. This thread will be copying the blocks from the external file to our datadir and then put them all into the block index. It additionally does not do ActivateBestChain until after all of the blocks have been loaded, so we could end up in a similar scenario as a reindex - all the blocks are written but the height still remains 0.

We need to be checking the disk space before -loadblk is handled so that we can know whether we have enough disk space before we begin loading blocks from the external file(s).

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 21, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK achow101, aureleoules, willcl-ark
Concept ACK hernanmarino, luke-jr, pablomartin4btc

Conflicts

No conflicts as of last run.

@hernanmarino
Copy link
Contributor

I agree with @achow101 I believe this should be done earlier.

Copy link
Member

@pablomartin4btc pablomartin4btc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @hernanmarino and @achow101, perhaps we could move that validation after the other 2 that also check disk space? (around line 1635)

@Empact Empact force-pushed the disk-space-check branch from 11fc52c to bba667e Compare August 8, 2022 05:28
@Empact
Copy link
Contributor Author

Empact commented Aug 8, 2022

As suggested, moved the check back to being alongside the other disk space checks.

Copy link
Contributor

@hernanmarino hernanmarino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK bba667e

@Empact
Copy link
Contributor Author

Empact commented Aug 8, 2022

Will need some accommodation for the tests - ignoring the message perhaps?:

test 2022-08-08T06:47:18.274000Z TestFramework (ERROR): Assertion failed
Traceback (most recent call last):
File "/tmp/cirrus-ci-build/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/test/functional/test_framework/test_framework.py", line 133, in main
self.run_test()
File "/tmp/cirrus-ci-build/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/test/functional/wallet_crosschain.py", line 53, in run_test
self.stop_nodes()
File "/tmp/cirrus-ci-build/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/test/functional/test_framework/test_framework.py", line 567, in stop_nodes
node.stop_node(wait=wait, wait_until_stopped=False)
File "/tmp/cirrus-ci-build/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/test/functional/test_framework/test_node.py", line 350, in stop_node
raise AssertionError("Unexpected stderr {} != {}".format(stderr, expected_stderr))
AssertionError: Unexpected stderr Warning: Disk space for "/tmp/cirrus-ci-build/ci/scratch/test_runner/test_runner_₿_🏃_20220808_063508/wallet_crosschain_80/node1/testnet3/blocks" may not accommodate the block files. Approximately 40 GB of data will be stored in this directory. !=

https://cirrus-ci.com/task/5510419358941184?logs=ci#L4359

Copy link
Member

@pablomartin4btc pablomartin4btc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core review ACK.

And it works as expected, only displaying the warning the first time before we start loading blocks.

2022-08-08T19:01:43Z Opening LevelDB in /tmp/btc/chainstate
2022-08-08T19:01:43Z Opened LevelDB successfully
2022-08-08T19:01:43Z Wrote new obfuscate key for /tmp/btc/chainstate: f4392c646d173853
2022-08-08T19:01:43Z Using obfuscation key for /tmp/btc/chainstate: f4392c646d173853
2022-08-08T19:01:43Z init message: Verifying blocks…
2022-08-08T19:01:43Z  block index              82ms
2022-08-08T19:01:43Z Warning: Disk space for "/tmp/btc/blocks" may not accommodate the block files. Approximately 460 GB of data will be stored in this directory.
2022-08-08T19:02:24Z loadblk thread start
2022-08-08T19:02:24Z UpdateTip: new best=000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f height=0 version=0x00000001 log2_work=32.000022 tx=1 date='2009-01-03T18:15:05Z' progress=0.000000 cache=0.0MiB(0txo)
2022-08-08T19:02:24Z block tree size = 1
2022-08-08T19:02:24Z nBestHeight = 0
2022-08-08T19:02:24Z Failed to open mempool file from disk. Continuing anyway.
2022-08-08T19:02:24Z loadblk thread exit

Also on the UI:

image

@pablomartin4btc
Copy link
Member

@Empact, I also get these 2 funtional tests failing when I run them locally (they pass on the master):

image

Copy link
Contributor

@aureleoules aureleoules left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept and Code Review ACK bba667e.
I also verified that the warning is shown.

CI fails because the tests p2p_dos_header_tree.py and wallet_crosschain.py try to spawn a testnet chain, which would be too big for the CI and creates an output on stderr which should not happen.

This fix works on my machine but I haven't tested in CI:
diff --git a/test/functional/p2p_dos_header_tree.py b/test/functional/p2p_dos_header_tree.py
index fde1e4bfa..922e65036 100755
--- a/test/functional/p2p_dos_header_tree.py
+++ b/test/functional/p2p_dos_header_tree.py
@@ -22,6 +22,7 @@ class RejectLowDifficultyHeadersTest(BitcoinTestFramework):
         self.setup_clean_chain = True
         self.chain = 'testnet3'  # Use testnet chain because it has an early checkpoint
         self.num_nodes = 2
+        self.extra_args = [['-prune=550']] * self.num_nodes
 
     def add_options(self, parser):
         parser.add_argument(
@@ -62,7 +63,7 @@ class RejectLowDifficultyHeadersTest(BitcoinTestFramework):
 
         self.log.info("Feed all fork headers (succeeds without checkpoint)")
         # On node 0 it succeeds because checkpoints are disabled
-        self.restart_node(0, extra_args=['-nocheckpoints'])
+        self.restart_node(0, extra_args=['-nocheckpoints', '-prune=550'])
         peer_no_checkpoint = self.nodes[0].add_p2p_connection(P2PInterface())
         peer_no_checkpoint.send_and_ping(msg_headers(self.headers_fork))
         assert {
diff --git a/test/functional/wallet_crosschain.py b/test/functional/wallet_crosschain.py
index b6d0c8798..c0047542e 100755
--- a/test/functional/wallet_crosschain.py
+++ b/test/functional/wallet_crosschain.py
@@ -21,7 +21,7 @@ class WalletCrossChain(BitcoinTestFramework):
 
         # Switch node 1 to testnet before starting it.
         self.nodes[1].chain = 'testnet3'
-        self.nodes[1].extra_args = ['-maxconnections=0'] # disable testnet sync
+        self.nodes[1].extra_args = ['-maxconnections=0', '-prune=550'] # disable testnet sync
         with open(self.nodes[1].bitcoinconf, 'r', encoding='utf8') as conf:
             conf_data = conf.read()
         with open (self.nodes[1].bitcoinconf, 'w', encoding='utf8') as conf:
@@ -51,7 +51,7 @@ class WalletCrossChain(BitcoinTestFramework):
         if not self.options.descriptors:
             self.log.info("Override cross-chain wallet load protection")
             self.stop_nodes()
-            self.start_nodes([['-walletcrosschain']] * self.num_nodes)
+            self.start_nodes([['-walletcrosschain', '-prune=550']] * self.num_nodes)
             self.nodes[0].loadwallet(node1_wallet)
             self.nodes[1].loadwallet(node0_wallet)

EDIT: it works in CI as well

@Empact Empact force-pushed the disk-space-check branch 3 times, most recently from 2845677 to 1732375 Compare October 10, 2022 20:34
To accommodate the expected blocks data.

Co-authored-by: Antoine Poinsot <darosior@protonmail.com>
Co-authored-by: benthecarman <benthecarman@live.com>
Co-authored-by: Justin Litchfield <litch@me.com>
Co-authored-by: Liran Cohen <c.liran.c@gmail.com>
Co-authored-by: Ryan Loomba <ryan.loomba@gmail.com>
Co-authored-by: Buck Perley <bucko.perley@gmail.com>
Co-authored-by: bajjer <bajjer@bajjer.xyz>
Co-authored-by: Suhail Saqan <suhail.saqan@gmail.com>
Co-authored-by: Christopher Sweeney <sweeney.chris@gmail.com>
Co-authored-by: Alyssa <orbitalturtle@protonmail.com>
Co-authored-by: Ben Schroth <ben@styng.social>
Co-authored-by: Jason Hester <mail@jason-hester.me>
Co-authored-by: Matt Clough <Matt.clough@pm.me>
Co-authored-by: Elise Schedler <eliseschedler@gmail.com>
Co-authored-by: ghander <cen254@gmail.com>
Co-authored-by: PopeLaz <btclz@fastmail.com>
Co-authored-by: Aurèle Oulès <hello@aureleoules.com>
Copy link
Contributor

@aureleoules aureleoules left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 6630a1e

@glozow glozow requested a review from mzumsande October 12, 2022 19:31
@achow101
Copy link
Member

cc @mzumsande @theStack

@achow101
Copy link
Member

ACK 6630a1e

@willcl-ark
Copy link
Member

tACK 6630a1e rebased on master. Warning shows on first start but not on restart after some blocks have been downloaded.

Copy link
Member

@pablomartin4btc pablomartin4btc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK 6630a1e

Copy link
Contributor

@hernanmarino hernanmarino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReACK 6630a1e

@achow101 achow101 merged commit aeb395d into bitcoin:master Nov 18, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Nov 18, 2022
@Empact
Copy link
Contributor Author

Empact commented Nov 18, 2022

Thanks to all the reviewers and contributors. For posterity, here's the original session in which this was written: https://youtu.be/tRXIkqM8gSI

@ghost ghost mentioned this pull request Mar 11, 2023
@bitcoin bitcoin locked and limited conversation to collaborators Jun 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should we check free disk size before syncing blocks