Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2022 The Bitcoin Core developers 3 : // Distributed under the MIT software license, see the accompanying 4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 : 6 : #ifndef BITCOIN_NODE_UTXO_SNAPSHOT_H 7 : #define BITCOIN_NODE_UTXO_SNAPSHOT_H 8 : 9 : #include <kernel/cs_main.h> 10 : #include <serialize.h> 11 : #include <sync.h> 12 : #include <uint256.h> 13 : #include <util/fs.h> 14 : 15 : #include <cstdint> 16 : #include <optional> 17 : #include <string_view> 18 : 19 : class Chainstate; 20 : 21 : namespace node { 22 : //! Metadata describing a serialized version of a UTXO set from which an 23 : //! assumeutxo Chainstate can be constructed. 24 : class SnapshotMetadata 25 : { 26 : public: 27 : //! The hash of the block that reflects the tip of the chain for the 28 : //! UTXO set contained in this snapshot. 29 : uint256 m_base_blockhash; 30 : 31 : //! The number of coins in the UTXO set contained in this snapshot. Used 32 : //! during snapshot load to estimate progress of UTXO set reconstruction. 33 0 : uint64_t m_coins_count = 0; 34 : 35 0 : SnapshotMetadata() { } 36 0 : SnapshotMetadata( 37 : const uint256& base_blockhash, 38 : uint64_t coins_count, 39 : unsigned int nchaintx) : 40 0 : m_base_blockhash(base_blockhash), 41 0 : m_coins_count(coins_count) { } 42 : 43 0 : SERIALIZE_METHODS(SnapshotMetadata, obj) { READWRITE(obj.m_base_blockhash, obj.m_coins_count); } 44 : }; 45 : 46 : //! The file in the snapshot chainstate dir which stores the base blockhash. This is 47 : //! needed to reconstruct snapshot chainstates on init. 48 : //! 49 : //! Because we only allow loading a single snapshot at a time, there will only be one 50 : //! chainstate directory with this filename present within it. 51 : const fs::path SNAPSHOT_BLOCKHASH_FILENAME{"base_blockhash"}; 52 : 53 : //! Write out the blockhash of the snapshot base block that was used to construct 54 : //! this chainstate. This value is read in during subsequent initializations and 55 : //! used to reconstruct snapshot-based chainstates. 56 : bool WriteSnapshotBaseBlockhash(Chainstate& snapshot_chainstate) 57 : EXCLUSIVE_LOCKS_REQUIRED(::cs_main); 58 : 59 : //! Read the blockhash of the snapshot base block that was used to construct the 60 : //! chainstate. 61 : std::optional<uint256> ReadSnapshotBaseBlockhash(fs::path chaindir) 62 : EXCLUSIVE_LOCKS_REQUIRED(::cs_main); 63 : 64 : //! Suffix appended to the chainstate (leveldb) dir when created based upon 65 : //! a snapshot. 66 : constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX = "_snapshot"; 67 : 68 : 69 : //! Return a path to the snapshot-based chainstate dir, if one exists. 70 : std::optional<fs::path> FindSnapshotChainstateDir(const fs::path& data_dir); 71 : 72 : } // namespace node 73 : 74 : #endif // BITCOIN_NODE_UTXO_SNAPSHOT_H