|
| 1 | +// Copyright (c) 2021 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#ifndef BITCOIN_NODE_CHAINSTATE_H |
| 6 | +#define BITCOIN_NODE_CHAINSTATE_H |
| 7 | + |
| 8 | +#include <cstdint> |
| 9 | +#include <functional> |
| 10 | +#include <memory> |
| 11 | +#include <optional> |
| 12 | +#include <string> |
| 13 | + |
| 14 | +class CActiveMasternodeManager; |
| 15 | +class CChainstateHelper; |
| 16 | +class CCreditPoolManager; |
| 17 | +class CDeterministicMNManager; |
| 18 | +class CEvoDB; |
| 19 | +class CGovernanceManager; |
| 20 | +class ChainstateManager; |
| 21 | +class CMasternodeMetaMan; |
| 22 | +class CMasternodeSync; |
| 23 | +class CMNHFManager; |
| 24 | +class CSporkManager; |
| 25 | +class CTxMemPool; |
| 26 | +struct LLMQContext; |
| 27 | + |
| 28 | +namespace llmq { |
| 29 | +class CChainLocksHandler; |
| 30 | +class CInstantSendManager; |
| 31 | +class CQuorumSnapshotManager; |
| 32 | +} |
| 33 | + |
| 34 | +namespace Consensus { |
| 35 | +struct Params; |
| 36 | +} |
| 37 | + |
| 38 | +enum class ChainstateLoadingError { |
| 39 | + ERROR_LOADING_BLOCK_DB, |
| 40 | + ERROR_BAD_GENESIS_BLOCK, |
| 41 | + ERROR_BAD_DEVNET_GENESIS_BLOCK, |
| 42 | + ERROR_TXINDEX_DISABLED_WHEN_GOV_ENABLED, |
| 43 | + ERROR_ADDRIDX_NEEDS_REINDEX, |
| 44 | + ERROR_SPENTIDX_NEEDS_REINDEX, |
| 45 | + ERROR_TIMEIDX_NEEDS_REINDEX, |
| 46 | + ERROR_PRUNED_NEEDS_REINDEX, |
| 47 | + ERROR_LOAD_GENESIS_BLOCK_FAILED, |
| 48 | + ERROR_CHAINSTATE_UPGRADE_FAILED, |
| 49 | + ERROR_REPLAYBLOCKS_FAILED, |
| 50 | + ERROR_LOADCHAINTIP_FAILED, |
| 51 | + ERROR_GENERIC_BLOCKDB_OPEN_FAILED, |
| 52 | + ERROR_COMMITING_EVO_DB, |
| 53 | + ERROR_UPGRADING_EVO_DB, |
| 54 | + ERROR_UPGRADING_SIGNALS_DB, |
| 55 | + SHUTDOWN_PROBED, |
| 56 | +}; |
| 57 | + |
| 58 | +/** This sequence can have 4 types of outcomes: |
| 59 | + * |
| 60 | + * 1. Success |
| 61 | + * 2. Shutdown requested |
| 62 | + * - nothing failed but a shutdown was triggered in the middle of the |
| 63 | + * sequence |
| 64 | + * 3. Soft failure |
| 65 | + * - a failure that might be recovered from with a reindex |
| 66 | + * 4. Hard failure |
| 67 | + * - a failure that definitively cannot be recovered from with a reindex |
| 68 | + * |
| 69 | + * Currently, LoadChainstate returns a std::optional<ChainstateLoadingError> |
| 70 | + * which: |
| 71 | + * |
| 72 | + * - if has_value() |
| 73 | + * - Either "Soft failure", "Hard failure", or "Shutdown requested", |
| 74 | + * differentiable by the specific enumerator. |
| 75 | + * |
| 76 | + * Note that a return value of SHUTDOWN_PROBED means ONLY that "during |
| 77 | + * this sequence, when we explicitly checked shutdown_requested() at |
| 78 | + * arbitrary points, one of those calls returned true". Therefore, a |
| 79 | + * return value other than SHUTDOWN_PROBED does not guarantee that |
| 80 | + * shutdown_requested() hasn't been called indirectly. |
| 81 | + * - else |
| 82 | + * - Success! |
| 83 | + */ |
| 84 | +std::optional<ChainstateLoadingError> LoadChainstate(bool fReset, |
| 85 | + ChainstateManager& chainman, |
| 86 | + CGovernanceManager& govman, |
| 87 | + CMasternodeMetaMan& mn_metaman, |
| 88 | + CMasternodeSync& mn_sync, |
| 89 | + CSporkManager& sporkman, |
| 90 | + std::unique_ptr<CActiveMasternodeManager>& mn_activeman, |
| 91 | + std::unique_ptr<CChainstateHelper>& chain_helper, |
| 92 | + std::unique_ptr<CCreditPoolManager>& cpoolman, |
| 93 | + std::unique_ptr<CDeterministicMNManager>& dmnman, |
| 94 | + std::unique_ptr<CEvoDB>& evodb, |
| 95 | + std::unique_ptr<CMNHFManager>& mnhf_manager, |
| 96 | + std::unique_ptr<llmq::CChainLocksHandler>& clhandler, |
| 97 | + std::unique_ptr<llmq::CInstantSendManager>& isman, |
| 98 | + std::unique_ptr<llmq::CQuorumSnapshotManager>& qsnapman, |
| 99 | + std::unique_ptr<LLMQContext>& llmq_ctx, |
| 100 | + CTxMemPool* mempool, |
| 101 | + bool fPruneMode, |
| 102 | + bool is_addrindex_enabled, |
| 103 | + bool is_governance_enabled, |
| 104 | + bool is_spentindex_enabled, |
| 105 | + bool is_timeindex_enabled, |
| 106 | + bool is_txindex_enabled, |
| 107 | + const Consensus::Params& consensus_params, |
| 108 | + const std::string& network_id, |
| 109 | + bool fReindexChainState, |
| 110 | + int64_t nBlockTreeDBCache, |
| 111 | + int64_t nCoinDBCache, |
| 112 | + int64_t nCoinCacheUsage, |
| 113 | + bool block_tree_db_in_memory, |
| 114 | + bool coins_db_in_memory, |
| 115 | + std::function<bool()> shutdown_requested = nullptr, |
| 116 | + std::function<void()> coins_error_cb = nullptr); |
| 117 | + |
| 118 | +/** Initialize Dash-specific components during chainstate initialization */ |
| 119 | +void DashChainstateSetup(ChainstateManager& chainman, |
| 120 | + CGovernanceManager& govman, |
| 121 | + CMasternodeMetaMan& mn_metaman, |
| 122 | + CMasternodeSync& mn_sync, |
| 123 | + CSporkManager& sporkman, |
| 124 | + std::unique_ptr<CActiveMasternodeManager>& mn_activeman, |
| 125 | + std::unique_ptr<CChainstateHelper>& chain_helper, |
| 126 | + std::unique_ptr<CCreditPoolManager>& cpoolman, |
| 127 | + std::unique_ptr<CDeterministicMNManager>& dmnman, |
| 128 | + std::unique_ptr<CEvoDB>& evodb, |
| 129 | + std::unique_ptr<CMNHFManager>& mnhf_manager, |
| 130 | + std::unique_ptr<llmq::CQuorumSnapshotManager>& qsnapman, |
| 131 | + std::unique_ptr<LLMQContext>& llmq_ctx, |
| 132 | + CTxMemPool* mempool, |
| 133 | + bool fReset, |
| 134 | + bool fReindexChainState, |
| 135 | + const Consensus::Params& consensus_params); |
| 136 | + |
| 137 | +void DashChainstateSetupClose(std::unique_ptr<CChainstateHelper>& chain_helper, |
| 138 | + std::unique_ptr<CCreditPoolManager>& cpoolman, |
| 139 | + std::unique_ptr<CDeterministicMNManager>& dmnman, |
| 140 | + std::unique_ptr<CMNHFManager>& mnhf_manager, |
| 141 | + std::unique_ptr<llmq::CQuorumSnapshotManager>& qsnapman, |
| 142 | + std::unique_ptr<LLMQContext>& llmq_ctx, |
| 143 | + CTxMemPool* mempool); |
| 144 | + |
| 145 | +enum class ChainstateLoadVerifyError { |
| 146 | + ERROR_BLOCK_FROM_FUTURE, |
| 147 | + ERROR_CORRUPTED_BLOCK_DB, |
| 148 | + ERROR_EVO_DB_SANITY_FAILED, |
| 149 | + ERROR_GENERIC_FAILURE, |
| 150 | +}; |
| 151 | + |
| 152 | +std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManager& chainman, |
| 153 | + CEvoDB& evodb, |
| 154 | + bool fReset, |
| 155 | + bool fReindexChainState, |
| 156 | + const Consensus::Params& consensus_params, |
| 157 | + unsigned int check_blocks, |
| 158 | + unsigned int check_level, |
| 159 | + std::function<int64_t()> get_unix_time_seconds, |
| 160 | + std::function<void(bool)> notify_bls_state = nullptr); |
| 161 | + |
| 162 | +#endif // BITCOIN_NODE_CHAINSTATE_H |
0 commit comments