Skip to content
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

assumeutxo, blockstorage: Prevent core dump on invalid hash #28698

Conversation

pablomartin4btc
Copy link
Member

@pablomartin4btc pablomartin4btc commented Oct 20, 2023

While reviewing #27596 (ran loadtxoutset in mainnet before m_assumeutxo_data is empty as currently in master - back to 1b1d711), got a core dumped, so it seems there's a potential issue if new releases ever remove snapshot details or a semi-experienced user performs a loadtxoutset on a different "customised" binary version (not sure if this is a real use case).

2023-10-18T17:42:52Z [init] Using obfuscation key for /tmp/.test_utxo_2/blocks/index: 0000000000000000
node/blockstorage.cpp:390 LoadBlockIndex: Assertion `GetParams().AssumeutxoForBlockhash(*snapshot_blockhash)' failed.
Aborted (core dumped)
This is also happening before IBD is completed (background validation still being performed as it can be seen in rpc getchainstates)
/src/bitcoin-cli -datadir=${AU_DATADIR} getchainstates
{
  "headers": 813097,
  "chainstates": [
    {
      "blocks": 368249,
      "bestblockhash": "00000000000000000b7a08224a1cb00d337100ba7a46c03d04b2c2d8964efc37",
      "difficulty": 52278304845.59168,
      "verificationprogress": 0.086288278873286,
      "coins_db_cache_bytes": 7969177,
      "coins_tip_cache_bytes": 14908338995,
      "validated": true
    },
    {
      "blocks": 813097,
      "bestblockhash": "0000000000000000000270c9fdce7b17db64cca91f90106964b58e33a4d91089",
      "difficulty": 61030681983175.59,
      "verificationprogress": 0.999997140098457,
      "coins_db_cache_bytes": 419430,
      "coins_tip_cache_bytes": 784649420,
      "snapshot_blockhash": "00000000000000000002a7c4c1e48d76c5a37902165a270156b7a8d72728a054",
      "validated": false
    }
  ]
}
Steps to reproduce the core dump error and its output:
  1. Perform a loadtxoutset in mainnet on compiled bitcoind adding the block hash from Sjors's commit.
  2. Once step 1 finishes, remove the added code from step 1 and compile again or just compile master without any changes on top.
  3. Run bitcoind, soon it'll crash with:
2023-10-18T17:42:52Z [init] init message: Loading block index…
2023-10-18T17:42:52Z [init] Assuming ancestors of block 00000000000000000001a0a448d6cf2546b06801389cc030b2b18c6491266815 have valid signatures.
2023-10-18T17:42:52Z [init] Setting nMinimumChainWork=000000000000000000000000000000000000000052b2559353df4117b7348b64
2023-10-18T17:42:52Z [init] Prune configured to target 3000 MiB on disk for block and undo files.
2023-10-18T17:42:52Z [init] [snapshot] detected active snapshot chainstate (/tmp/.test_utxo_2/chainstate_snapshot) - loading
2023-10-18T17:42:52Z [init] [snapshot] switching active chainstate to Chainstate [snapshot] @ height -1 (null)
2023-10-18T17:42:52Z [init] Opening LevelDB in /tmp/.test_utxo_2/blocks/index
2023-10-18T17:42:52Z [init] Opened LevelDB successfully
2023-10-18T17:42:52Z [init] Using obfuscation key for /tmp/.test_utxo_2/blocks/index: 0000000000000000
node/blockstorage.cpp:390 LoadBlockIndex: Assertion `GetParams().AssumeutxoForBlockhash(*snapshot_blockhash)' failed.
Aborted (core dumped)
After original change, error message output:
2023-10-20T15:49:12Z [init] init message: Loading block index…
2023-10-20T15:49:12Z [init] Assuming ancestors of block 00000000000000000001a0a448d6cf2546b06801389cc030b2b18c6491266815 have valid signatures.
2023-10-20T15:49:12Z [init] Setting nMinimumChainWork=000000000000000000000000000000000000000052b2559353df4117b7348b64
2023-10-20T15:49:12Z [init] Prune configured to target 3000 MiB on disk for block and undo files.
2023-10-20T15:49:12Z [init] [snapshot] detected active snapshot chainstate (/tmp/.test_utxo_2/chainstate_snapshot) - loading
2023-10-20T15:49:12Z [init] [snapshot] switching active chainstate to Chainstate [snapshot] @ height -1 (null)
2023-10-20T15:49:12Z [init] Opening LevelDB in /tmp/.test_utxo_2/blocks/index
2023-10-20T15:49:12Z [init] Opened LevelDB successfully
2023-10-20T15:49:12Z [init] Using obfuscation key for /tmp/.test_utxo_2/blocks/index: 0000000000000000
2023-10-20T15:49:13Z [init] *** Assumeutxo data not found for the given blockhash '00000000000000000002a7c4c1e48d76c5a37902165a270156b7a8d72728a054'.
2023-10-20T15:49:13Z [init] Error: Assumeutxo data not found for the given blockhash '00000000000000000002a7c4c1e48d76c5a37902165a270156b7a8d72728a054'.
Error: Assumeutxo data not found for the given blockhash '00000000000000000002a7c4c1e48d76c5a37902165a270156b7a8d72728a054'.
2023-10-20T15:49:13Z [init] Shutdown requested. Exiting.
2023-10-20T15:49:13Z [init] Shutdown: In progress...
2023-10-20T15:49:13Z [scheduler] scheduler thread exit
2023-10-20T15:49:13Z [shutoff] Flushed fee estimates to fee_estimates.dat.
2023-10-20T15:49:13Z [shutoff] Shutdown: done
Alternative on error handling using return error() instead of return FatalError() used in this PR, which produces a different output and perhaps confusing:
2023-10-20T21:45:58Z [init] Using obfuscation key for /tmp/.test_utxo_2/blocks/index: 0000000000000000
2023-10-20T21:45:59Z [init] ERROR: Assumeutxo data not found for the given blockhash '00000000000000000002a7c4c1e48d76c5a37902165a270156b7a8d72728a054'.
2023-10-20T21:45:59Z [init] : Error loading block database.
Please restart with -reindex or -reindex-chainstate to recover.
: Error loading block database.
Please restart with -reindex or -reindex-chainstate to recover.
2023-10-20T21:45:59Z [init] Aborted block database rebuild. Exiting.
2023-10-20T21:45:59Z [init] Shutdown: In progress...
2023-10-20T21:45:59Z [scheduler] scheduler thread exit
2023-10-20T21:45:59Z [shutoff] Flushed fee estimates to fee_estimates.dat.
2023-10-20T21:45:59Z [shutoff] Shutdown: done
Current state (including ryanofsky suggestion), after code change, error message output:
2023-10-25T02:29:57Z [init] Using obfuscation key for /home/pablo/.test_utxo_2/regtest/blocks/index: 0000000000000000
2023-10-25T02:29:57Z [init] *** Assumeutxo data not found for the given blockhash 'f09b5835f3f8b39481f2af3257bbc2e82845552d4d2d6d31cf520fc24263ed5b'.
2023-10-25T02:29:57Z [init] Error: A fatal internal error occurred, see debug.log for details
Error: A fatal internal error occurred, see debug.log for details
2023-10-25T02:29:57Z [init] Shutdown requested. Exiting.
2023-10-25T02:29:57Z [init] Shutdown: In progress...
2023-10-25T02:29:57Z [scheduler] scheduler thread exit
2023-10-25T02:29:57Z [shutoff] Flushed fee estimates to fee_estimates.dat.
2023-10-25T02:29:57Z [shutoff] Shutdown: done

@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 20, 2023

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

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK theStack, ryanofsky, naumenkogs
Stale ACK Sjors, fjahr

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

@theStack
Copy link
Contributor

Concept ACK

I think in practice the most likely scenario for triggering this condition is that a user first successfully loads an UTXO snapshot with AssumeUTXO hash H in release version Y, and at some later point runs an earlier release version X on that same datadir (i.e. X < Y and Y includes H in the AssumeUTXO parameters, but X doesn't yet). This will probably not happen too often, but nevertheless the crash should be fixed.

Copy link
Member

@Sjors Sjors left a comment

Choose a reason for hiding this comment

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

Concept ACK

caae1c9 looks good, just two suggestions

I haven't tried reproducing the crash or the fix.

src/node/blockstorage.cpp Outdated Show resolved Hide resolved
src/node/blockstorage.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@ryanofsky ryanofsky 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 caae1c9

Nice catch. It would be good to have a python test for this. A test can trigger the crash / error by just creating a chainstate_snapshot/base_blockhash file in the datadir with a random block hash.

From the command line this is also easy to test by running:

mkdir ~/.bitcoin/regtest/chainstate_snapshot
dd bs=32 count=1 if=/dev/urandom of=~/.bitcoin/regtest/chainstate_snapshot/base_blockhash
bitcoin -regtest

src/node/blockstorage.cpp Outdated Show resolved Hide resolved
@theStack
Copy link
Contributor

Nice catch. It would be good to have a python test for this. A test can trigger the crash / error by just creating a chainstate_snapshot/base_blockhash file in the datadir with a random block hash.

I've written a test using this idea, based on this PR on commit theStack@fb4eb16 (branch https://github.com/theStack/bitcoin/commits/pr28698_test_followup), feel free to include it here @pablomartin4btc.

@pablomartin4btc
Copy link
Member Author

From the command line this is also easy to test by running:

True, tested it, thanks @ryanofsky!
dd bs=32 count=1 if=/dev/urandom of=${AU_DATADIR}/regtest/chainstate_snapshot/base_blockhash
1+0 records in
1+0 records out
32 bytes copied, 7,6874e-05 s, 416 kB/s
./src/bitcoind -datadir=${AU_DATADIR} -regtest
2023-10-24T23:13:19Z [init] Using obfuscation key for /home/pablo/.test_utxo_2/regtest/blocks/index: 0000000000000000
2023-10-24T23:13:19Z [init] *** Assumeutxo data not found for the given blockhash 'f09b5835f3f8b39481f2af3257bbc2e82845552d4d2d6d31cf520fc24263ed5b'.
2023-10-24T23:13:19Z [init] Error: Assumeutxo data not found for the given blockhash 'f09b5835f3f8b39481f2af3257bbc2e82845552d4d2d6d31cf520fc24263ed5b'.
Error: Assumeutxo data not found for the given blockhash 'f09b5835f3f8b39481f2af3257bbc2e82845552d4d2d6d31cf520fc24263ed5b'.
2023-10-24T23:13:19Z [init] Shutdown requested. Exiting.
2023-10-24T23:13:19Z [init] Shutdown: In progress...
2023-10-24T23:13:19Z [scheduler] scheduler thread exit
2023-10-24T23:13:19Z [shutoff] Flushed fee estimates to fee_estimates.dat.
2023-10-24T23:13:19Z [shutoff] Shutdown: done

@pablomartin4btc pablomartin4btc force-pushed the assumeutxo-safer-exit-on-init-core-dumped branch from caae1c9 to 3d7c103 Compare October 25, 2023 03:08
@pablomartin4btc
Copy link
Member Author

Rebased.

I've also included suggestions from @Sjors, @ryanofsky and functional test from @theStack. Thanks to all for reviewing!

@Sjors
Copy link
Member

Sjors commented Oct 25, 2023

ACK 3d7c103

I tested that reverting 4a5be10 breaks the new test, and shows the original Assertion.

@DrahtBot DrahtBot requested review from ryanofsky and removed request for Sjors October 25, 2023 08:02
Copy link
Contributor

@ryanofsky ryanofsky 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 3d7c103. New test is nice and implementation is a little cleaner

test/functional/feature_assumeutxo.py Outdated Show resolved Hide resolved
@fjahr
Copy link
Contributor

fjahr commented Oct 25, 2023

Code review ACK 3d7c103

…rameters

Co-authored-by: Russell Yanofsky <russ@yanofsky.org>
Co-authored-by: Sebastian Falbesoner <sebastian.falbesoner@gmail.com>
@pablomartin4btc pablomartin4btc force-pushed the assumeutxo-safer-exit-on-init-core-dumped branch from 3d7c103 to 811067c Compare October 25, 2023 14:15
Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

ACK 811067c

nit, if you have to retouch: probably it would be good to also check for the specific error message ("Assumeutxo data not found for the given blockhash...") via assert_debug_log, rather than only the generic "fatal internal error" one which goes to stderr.

@Sjors
Copy link
Member

Sjors commented Oct 26, 2023

Agree with @theStack's suggestion, forgot to say that yesterday.

Copy link
Contributor

@ryanofsky ryanofsky 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 811067c.

Just suggested test simplifications since last review. I also agree the suggestion to check specifically for the right error message would be nice.

@naumenkogs
Copy link
Member

ACK 811067c

@fanquake fanquake merged commit feae4e0 into bitcoin:master Oct 29, 2023
16 checks passed
fanquake pushed a commit to fanquake/bitcoin that referenced this pull request Oct 30, 2023
fanquake pushed a commit to fanquake/bitcoin that referenced this pull request Oct 30, 2023
…rameters

Co-authored-by: Russell Yanofsky <russ@yanofsky.org>
Co-authored-by: Sebastian Falbesoner <sebastian.falbesoner@gmail.com>

Github-Pull: bitcoin#28698
Reabsed-From: 811067c
@fanquake
Copy link
Member

Backported this to 26.x in #28754.

fanquake pushed a commit to fanquake/bitcoin that referenced this pull request Oct 31, 2023
fanquake pushed a commit to fanquake/bitcoin that referenced this pull request Oct 31, 2023
…rameters

Co-authored-by: Russell Yanofsky <russ@yanofsky.org>
Co-authored-by: Sebastian Falbesoner <sebastian.falbesoner@gmail.com>

Github-Pull: bitcoin#28698
Reabsed-From: 811067c
fanquake added a commit that referenced this pull request Nov 1, 2023
e4e8479 doc: update manual pages for v26.0rc2 (fanquake)
0b189a9 build: bump version to v26.0rc2 (fanquake)
e097d4c gui: fix crash on selecting "Mask values" in transaction view (Sebastian Falbesoner)
05e8874 guix: update signapple (fanquake)
deccc50 guix: Zip needs to include all files with time as SOURCE_DATE_EPOCH (Andrew Chow)
fe57abd test: add coverage for snapshot chainstate not matching AssumeUTXO parameters (pablomartin4btc)
b761a58 assumeutxo, blockstorage: prevent core dump on invalid hash (pablomartin4btc)
d3ebf6e [test] Test i2p private key constraints (Vasil Dimov)
1f11784 [net] Check i2p private key constraints (dergoegge)
6544ffa bugfix: Mark CNoDestination and PubKeyDestination constructor explicit (MarcoFalke)

Pull request description:

  Backports for v26.0rc2:
  * #28695
  * #28698
  * #28728
  * #28757
  * #28759
  * bitcoin-core/gui#774

ACKs for top commit:
  josibake:
    ACK e4e8479
  hebasto:
    re-ACK e4e8479, only a backport of bitcoin-core/gui#774 added since my [recent](#28754 (review)) review.
  TheCharlatan:
    Re-ACK e4e8479

Tree-SHA512: 4b95afd26b8bf91250cb883423de8b274cefa48dc474734f5900aeb756eee3a6c656116efcfa2caff3c250678c16b70cc6b7a5d840018dc7e2c1e8161622cd61
pablomartin4btc added a commit to pablomartin4btc/bitcoin that referenced this pull request Nov 9, 2023
fanquake added a commit to bitcoin-core/gui that referenced this pull request Nov 10, 2023
…bug_log on the assumeutxo invalid hash dump - follow-up #28698

7de7685 test, assumeutxo: Use assert_debug_log for error details (pablomartin4btc)

Pull request description:

  This is a follow-up on the invalid hash dump fix #28698, [suggested](bitcoin/bitcoin#28698 (review)) by theStack and agreed by Sjors and ryanofsky.

ACKs for top commit:
  Sjors:
    ACK 7de7685
  maflcko:
    lgtm ACK 7de7685

Tree-SHA512: 036b3cef3084e3ead8923e8dcabe4fa7ebe97fb514d223aa38bc38df10337e3fe3113e42322178b58fb03fcd4511af4b5b56bceecbb7ded5b9758842c70db3f2
janus pushed a commit to BitgesellOfficial/bitgesell that referenced this pull request Apr 1, 2024
This is a follow-up on the invalid hash dump fix PR #28698.

bitcoin/bitcoin#28698 (review)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

9 participants