| 6811 | } |
| 6812 | |
| 6813 | bool ChainstateManager::PopulateAndValidateSnapshot( |
| 6814 | Chainstate &snapshot_chainstate, AutoFile &coins_file, |
| 6815 | const SnapshotMetadata &metadata) { |
| 6816 | // It's okay to release cs_main before we're done using `coins_cache` |
| 6817 | // because we know that nothing else will be referencing the newly created |
| 6818 | // snapshot_chainstate yet. |
| 6819 | CCoinsViewCache &coins_cache = |
| 6820 | *WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsTip()); |
| 6821 | |
| 6822 | BlockHash base_blockhash = metadata.m_base_blockhash; |
| 6823 | |
| 6824 | CBlockIndex *snapshot_start_block = WITH_LOCK( |
| 6825 | ::cs_main, return m_blockman.LookupBlockIndex(base_blockhash)); |
| 6826 | |
| 6827 | if (!snapshot_start_block) { |
| 6828 | // Needed for ComputeUTXOStats to determine the |
| 6829 | // height and to avoid a crash when base_blockhash.IsNull() |
| 6830 | LogPrintf("[snapshot] Did not find snapshot start blockheader %s\n", |
| 6831 | base_blockhash.ToString()); |
| 6832 | return false; |
| 6833 | } |
| 6834 | |
| 6835 | int base_height = snapshot_start_block->nHeight; |
| 6836 | const auto &maybe_au_data = GetParams().AssumeutxoForHeight(base_height); |
| 6837 | |
| 6838 | if (!maybe_au_data) { |
| 6839 | LogPrintf("[snapshot] assumeutxo height in snapshot metadata not " |
| 6840 | "recognized (%d) - refusing to load snapshot\n", |
| 6841 | base_height); |
| 6842 | return false; |
| 6843 | } |
| 6844 | |
| 6845 | const AssumeutxoData &au_data = *maybe_au_data; |
| 6846 | |
| 6847 | // This work comparison is a duplicate check with the one performed later in |
| 6848 | // ActivateSnapshot(), but is done so that we avoid doing the long work of |
| 6849 | // staging a snapshot that isn't actually usable. |
| 6850 | if (WITH_LOCK(::cs_main, return !CBlockIndexWorkComparator()( |
| 6851 | ActiveTip(), snapshot_start_block))) { |
| 6852 | LogPrintf("[snapshot] activation failed - work does not exceed active " |
| 6853 | "chainstate\n"); |
| 6854 | return false; |
| 6855 | } |
| 6856 | |
| 6857 | const uint64_t coins_count = metadata.m_coins_count; |
| 6858 | uint64_t coins_left = metadata.m_coins_count; |
| 6859 | |
| 6860 | LogPrintf("[snapshot] loading %d coins from snapshot %s\n", coins_left, |
| 6861 | base_blockhash.ToString()); |
| 6862 | int64_t coins_processed{0}; |
| 6863 | |
| 6864 | while (coins_left > 0) { |
| 6865 | try { |
| 6866 | TxId txid; |
| 6867 | coins_file >> txid; |
| 6868 | size_t coins_per_txid{0}; |
| 6869 | coins_per_txid = ReadCompactSize(coins_file); |
| 6870 |
no test coverage detected