|
| 1 | +// Copyright (c) 2024 - present 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 | + |
| 6 | +// #include <node/loadsnapshot.h> |
| 7 | + |
| 8 | +#include <logging.h> |
| 9 | +#include <node/blockstorage.h> |
| 10 | +#include <node/context.h> |
| 11 | +#include <node/utxo_snapshot.h> |
| 12 | +#include <streams.h> |
| 13 | +#include <validation.h> |
| 14 | + |
| 15 | +#include <util/fs.h> |
| 16 | + |
| 17 | +#include <util/any.h> |
| 18 | +#include <string> |
| 19 | + |
| 20 | + |
| 21 | +using node::BlockManager; |
| 22 | +using node::NodeContext; |
| 23 | +using node::SnapshotMetadata; |
| 24 | + |
| 25 | +struct CUpdatedBlock |
| 26 | +{ |
| 27 | + uint256 hash; |
| 28 | + int height; |
| 29 | +}; |
| 30 | + |
| 31 | +bool ChainstateManager::LoadSnapshot(NodeContext& node, const std::string& path_string) |
| 32 | +{ |
| 33 | + fs::path path(fs::u8path(path_string)); |
| 34 | + if (!fs::exists(path)) { |
| 35 | + LogPrintf("[loadsnapshot] snapshot file %s does not exist\n", path.u8string()); |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + FILE* file{fsbridge::fopen(path, "rb")}; |
| 40 | + |
| 41 | + AutoFile afile{file}; |
| 42 | + if (afile.IsNull()) { |
| 43 | + LogPrintf("[loadsnapshot] failed to open snapshot file %s\n", path_string); |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + // Read the snapshot metadata. |
| 48 | + SnapshotMetadata metadata; |
| 49 | + afile >> metadata; |
| 50 | + |
| 51 | + // Get the base blockhash and look up the corresponding CBlockIndex object. |
| 52 | + uint256 base_blockhash = metadata.m_base_blockhash; |
| 53 | + int max_secs_to_wait_for_headers = 60 * 10; |
| 54 | + CBlockIndex* snapshot_start_block = nullptr; |
| 55 | + |
| 56 | + LogPrintf("[loadsnapshot] waiting to see blockheader %s in headers chain before snapshot activation\n", |
| 57 | + base_blockhash.ToString()); |
| 58 | + |
| 59 | + if (node.chainman == nullptr) { |
| 60 | + // Handle error |
| 61 | + LogPrintf("[loadsnapshot] node.chainman is null\n"); |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + ChainstateManager& chainman = *node.chainman; |
| 66 | + |
| 67 | + // snapshot_start_block = chainman.m_blockman.LookupBlockIndex(base_blockhash); |
| 68 | + |
| 69 | + // if (!snapshot_start_block) { |
| 70 | + // LogPrintf("[loadsnapshot] can't find blockheader %s\n", |
| 71 | + // base_blockhash.ToString()); |
| 72 | + // return false; |
| 73 | + // } |
| 74 | + |
| 75 | + while (max_secs_to_wait_for_headers > 0) { |
| 76 | + LogPrintf("[loadsnapshot] base_blockhash = %s\n", base_blockhash.ToString()); |
| 77 | + snapshot_start_block = WITH_LOCK(::cs_main, |
| 78 | + return chainman.m_blockman.LookupBlockIndex(base_blockhash)); |
| 79 | + max_secs_to_wait_for_headers -= 1; |
| 80 | + |
| 81 | + if (!snapshot_start_block) { |
| 82 | + std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 83 | + } else { |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // // snapshot_start_block = chainman.m_blockman.LookupBlockIndex(base_blockhash); |
| 89 | + |
| 90 | + if (!snapshot_start_block) { |
| 91 | + LogPrintf("[loadsnapshot] timed out waiting for snapshot start blockheader %s\n", |
| 92 | + base_blockhash.ToString()); |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + // Activate the snapshot. |
| 97 | + if (!chainman.ActivateSnapshot(afile, metadata, false)) { |
| 98 | + // std::string error_message = "Unable to load UTXO snapshot " + path.u8string(); |
| 99 | + // throw std::runtime_error(error_message); |
| 100 | + LogPrintf("[loadsnapshot] Unable to load UTXO snapshot %s\n", path.u8string()); |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + // Get the new tip and print a log message. |
| 105 | + CBlockIndex* new_tip{WITH_LOCK(::cs_main, return chainman.ActiveTip())}; |
| 106 | + LogPrintf("[loadsnashot] Loaded %d coins from snapshot %s at height %d\n", |
| 107 | + metadata.m_coins_count, new_tip->GetBlockHash().ToString(), new_tip->nHeight); |
| 108 | + |
| 109 | + return true; |
| 110 | +} |
0 commit comments