forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge bitcoin#25667: assumeutxo: snapshot initialization
bf95976 doc: add note about snapshot chainstate init (James O'Beirne) e4d7995 test: add testcases for snapshot initialization (James O'Beirne) cced4e7 test: move-only-ish: factor out LoadVerifyActivateChainstate() (James O'Beirne) 51fc924 test: allow on-disk coins and block tree dbs in tests (James O'Beirne) 3c36139 test: add reset_chainstate parameter for snapshot unittests (James O'Beirne) 00b357c validation: add ResetChainstates() (James O'Beirne) 3a29dfb move-only: test: make snapshot chainstate setup reusable (James O'Beirne) 8153bd9 blockmanager: avoid undefined behavior during FlushBlockFile (James O'Beirne) ad67ff3 validation: remove snapshot datadirs upon validation failure (James O'Beirne) 34d1590 add utilities for deleting on-disk leveldb data (James O'Beirne) 252abd1 init: add utxo snapshot detection (James O'Beirne) f9f1735 validation: rename snapshot chainstate dir (James O'Beirne) d14bebf db: add StoragePath to CDBWrapper/CCoinsViewDB (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11) (parent PR: bitcoin#15606) --- Half of the replacement for bitcoin#24232. The original PR grew larger than expected throughout the review process. This change adds the ability to initialize a snapshot-based chainstate during init if one is detected on disk. This is of course unused as of now (aside from in unittests) given that we haven't yet enabled actually loading snapshots. Don't be scared! There are some big move-only commits in here. Accompanying changes include: - moving the snapshot coinsdb directory from being called `chainstate_[base blockhash]` to `chainstate_snapshot`, since we only support one snapshot in use at a time. This simplifies some logic, but it necessitates writing that base blockhash out to a file within the coinsdb dir. See [discussion here](bitcoin#24232 (comment)). - adding a simple fix in `FlushBlockFile()` that avoids a crash when attemping to flush to disk before `LoadBlockIndexDB()` is called, which happens when calling `MaybeRebalanceCaches()` during multiple chainstate init. - improving the unittest to allow testing with on-disk chainstates - necessary to test a simulated restart and re-initialization. ACKs for top commit: naumenkogs: utACK bf95976 ariard: Code Review ACK bf95976 ryanofsky: Code review ACK bf95976. Changes since last review: rebasing, switching from CAutoFile to AutoFile, adding comments, switching from BOOST_CHECK to Assert in test util, using chainman.GetMutex() in tests, destroying one ChainstateManager before creating a new one in tests fjahr: utACK bf95976 aureleoules: ACK bf95976 Tree-SHA512: 15ae75caf19f8d12a12d2647c52897904d27b265a7af6b4ae7b858592eeadb8f9da6c2394b6baebec90adc28742c053e3eb506119577dae7c1e722ebb3b7bcc0
- Loading branch information
Showing
19 changed files
with
662 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) 2022 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <node/utxo_snapshot.h> | ||
|
||
#include <fs.h> | ||
#include <logging.h> | ||
#include <streams.h> | ||
#include <uint256.h> | ||
#include <util/system.h> | ||
#include <validation.h> | ||
|
||
#include <cstdio> | ||
#include <optional> | ||
|
||
namespace node { | ||
|
||
bool WriteSnapshotBaseBlockhash(Chainstate& snapshot_chainstate) | ||
{ | ||
AssertLockHeld(::cs_main); | ||
assert(snapshot_chainstate.m_from_snapshot_blockhash); | ||
|
||
const std::optional<fs::path> chaindir = snapshot_chainstate.CoinsDB().StoragePath(); | ||
assert(chaindir); // Sanity check that chainstate isn't in-memory. | ||
const fs::path write_to = *chaindir / node::SNAPSHOT_BLOCKHASH_FILENAME; | ||
|
||
FILE* file{fsbridge::fopen(write_to, "wb")}; | ||
AutoFile afile{file}; | ||
if (afile.IsNull()) { | ||
LogPrintf("[snapshot] failed to open base blockhash file for writing: %s\n", | ||
fs::PathToString(write_to)); | ||
return false; | ||
} | ||
afile << *snapshot_chainstate.m_from_snapshot_blockhash; | ||
|
||
if (afile.fclose() != 0) { | ||
LogPrintf("[snapshot] failed to close base blockhash file %s after writing\n", | ||
fs::PathToString(write_to)); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
std::optional<uint256> ReadSnapshotBaseBlockhash(fs::path chaindir) | ||
{ | ||
if (!fs::exists(chaindir)) { | ||
LogPrintf("[snapshot] cannot read base blockhash: no chainstate dir " /* Continued */ | ||
"exists at path %s\n", fs::PathToString(chaindir)); | ||
return std::nullopt; | ||
} | ||
const fs::path read_from = chaindir / node::SNAPSHOT_BLOCKHASH_FILENAME; | ||
const std::string read_from_str = fs::PathToString(read_from); | ||
|
||
if (!fs::exists(read_from)) { | ||
LogPrintf("[snapshot] snapshot chainstate dir is malformed! no base blockhash file " /* Continued */ | ||
"exists at path %s. Try deleting %s and calling loadtxoutset again?\n", | ||
fs::PathToString(chaindir), read_from_str); | ||
return std::nullopt; | ||
} | ||
|
||
uint256 base_blockhash; | ||
FILE* file{fsbridge::fopen(read_from, "rb")}; | ||
AutoFile afile{file}; | ||
if (afile.IsNull()) { | ||
LogPrintf("[snapshot] failed to open base blockhash file for reading: %s\n", | ||
read_from_str); | ||
return std::nullopt; | ||
} | ||
afile >> base_blockhash; | ||
|
||
if (std::fgetc(afile.Get()) != EOF) { | ||
LogPrintf("[snapshot] warning: unexpected trailing data in %s\n", read_from_str); | ||
} else if (std::ferror(afile.Get())) { | ||
LogPrintf("[snapshot] warning: i/o error reading %s\n", read_from_str); | ||
} | ||
return base_blockhash; | ||
} | ||
|
||
std::optional<fs::path> FindSnapshotChainstateDir() | ||
{ | ||
fs::path possible_dir = | ||
gArgs.GetDataDirNet() / fs::u8path(strprintf("chainstate%s", SNAPSHOT_CHAINSTATE_SUFFIX)); | ||
|
||
if (fs::exists(possible_dir)) { | ||
return possible_dir; | ||
} | ||
return std::nullopt; | ||
} | ||
|
||
} // namespace node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.