1
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
- // Copyright (c) 2009-2022 The Bitcoin Core developers
2
+ // Copyright (c) 2009-present The Bitcoin Core developers
3
3
// Distributed under the MIT software license, see the accompanying
4
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
@@ -5681,7 +5681,7 @@ util::Result<void> ChainstateManager::ActivateSnapshot(
5681
5681
}
5682
5682
5683
5683
if (!m_best_header || m_best_header->GetAncestor (base_blockheight) != snapshot_start_block) {
5684
- return util::Error{_ (" A forked headers-chain with more work than the chain with the snapshot base block header exists. Please proceed to sync without AssumeUtxo." )};
5684
+ return util::Error{Untranslated (" A forked headers-chain with more work than the chain with the snapshot base block header exists. Please proceed to sync without AssumeUtxo." )};
5685
5685
}
5686
5686
5687
5687
auto mempool{m_active_chainstate->GetMempool ()};
@@ -5735,7 +5735,7 @@ util::Result<void> ChainstateManager::ActivateSnapshot(
5735
5735
static_cast <size_t >(current_coinstip_cache_size * SNAPSHOT_CACHE_PERC));
5736
5736
}
5737
5737
5738
- auto cleanup_bad_snapshot = [&](bilingual_str&& reason) EXCLUSIVE_LOCKS_REQUIRED (::cs_main) {
5738
+ auto cleanup_bad_snapshot = [&](bilingual_str reason) EXCLUSIVE_LOCKS_REQUIRED (::cs_main) {
5739
5739
this ->MaybeRebalanceCaches ();
5740
5740
5741
5741
// PopulateAndValidateSnapshot can return (in error) before the leveldb datadir
@@ -5754,9 +5754,9 @@ util::Result<void> ChainstateManager::ActivateSnapshot(
5754
5754
return util::Error{std::move (reason)};
5755
5755
};
5756
5756
5757
- if (! this ->PopulateAndValidateSnapshot (*snapshot_chainstate, coins_file, metadata)) {
5757
+ if (auto res{ this ->PopulateAndValidateSnapshot (*snapshot_chainstate, coins_file, metadata)}; !res ) {
5758
5758
LOCK (::cs_main);
5759
- return cleanup_bad_snapshot (Untranslated (" population failed" ));
5759
+ return cleanup_bad_snapshot (strprintf ( Untranslated (" Population failed: %s " ), util::ErrorString (res) ));
5760
5760
}
5761
5761
5762
5762
LOCK (::cs_main); // cs_main required for rest of snapshot activation.
@@ -5821,7 +5821,7 @@ static void SnapshotUTXOHashBreakpoint(const util::SignalInterrupt& interrupt)
5821
5821
if (interrupt) throw StopHashingException ();
5822
5822
}
5823
5823
5824
- bool ChainstateManager::PopulateAndValidateSnapshot (
5824
+ util::Result< void > ChainstateManager::PopulateAndValidateSnapshot (
5825
5825
Chainstate& snapshot_chainstate,
5826
5826
AutoFile& coins_file,
5827
5827
const SnapshotMetadata& metadata)
@@ -5837,18 +5837,16 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
5837
5837
if (!snapshot_start_block) {
5838
5838
// Needed for ComputeUTXOStats to determine the
5839
5839
// height and to avoid a crash when base_blockhash.IsNull()
5840
- LogPrintf (" [snapshot] Did not find snapshot start blockheader %s\n " ,
5841
- base_blockhash.ToString ());
5842
- return false ;
5840
+ return util::Error{strprintf (Untranslated (" Did not find snapshot start blockheader %s" ),
5841
+ base_blockhash.ToString ())};
5843
5842
}
5844
5843
5845
5844
int base_height = snapshot_start_block->nHeight ;
5846
5845
const auto & maybe_au_data = GetParams ().AssumeutxoForHeight (base_height);
5847
5846
5848
5847
if (!maybe_au_data) {
5849
- LogPrintf (" [snapshot] assumeutxo height in snapshot metadata not recognized "
5850
- " (%d) - refusing to load snapshot\n " , base_height);
5851
- return false ;
5848
+ return util::Error{strprintf (Untranslated (" Assumeutxo height in snapshot metadata not recognized "
5849
+ " (%d) - refusing to load snapshot" ), base_height)};
5852
5850
}
5853
5851
5854
5852
const AssumeutxoData& au_data = *maybe_au_data;
@@ -5857,8 +5855,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
5857
5855
// ActivateSnapshot(), but is done so that we avoid doing the long work of staging
5858
5856
// a snapshot that isn't actually usable.
5859
5857
if (WITH_LOCK (::cs_main, return !CBlockIndexWorkComparator ()(ActiveTip (), snapshot_start_block))) {
5860
- LogPrintf (" [snapshot] activation failed - work does not exceed active chainstate\n " );
5861
- return false ;
5858
+ return util::Error{Untranslated (" Work does not exceed active chainstate" )};
5862
5859
}
5863
5860
5864
5861
const uint64_t coins_count = metadata.m_coins_count ;
@@ -5875,8 +5872,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
5875
5872
coins_per_txid = ReadCompactSize (coins_file);
5876
5873
5877
5874
if (coins_per_txid > coins_left) {
5878
- LogPrintf (" [snapshot] mismatch in coins count in snapshot metadata and actual snapshot data\n " );
5879
- return false ;
5875
+ return util::Error{Untranslated (" Mismatch in coins count in snapshot metadata and actual snapshot data" )};
5880
5876
}
5881
5877
5882
5878
for (size_t i = 0 ; i < coins_per_txid; i++) {
@@ -5888,14 +5884,12 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
5888
5884
if (coin.nHeight > base_height ||
5889
5885
outpoint.n >= std::numeric_limits<decltype (outpoint.n )>::max () // Avoid integer wrap-around in coinstats.cpp:ApplyHash
5890
5886
) {
5891
- LogPrintf (" [snapshot] bad snapshot data after deserializing %d coins\n " ,
5892
- coins_count - coins_left);
5893
- return false ;
5887
+ return util::Error{strprintf (Untranslated (" Bad snapshot data after deserializing %d coins" ),
5888
+ coins_count - coins_left)};
5894
5889
}
5895
5890
if (!MoneyRange (coin.out .nValue )) {
5896
- LogPrintf (" [snapshot] bad snapshot data after deserializing %d coins - bad tx out value\n " ,
5897
- coins_count - coins_left);
5898
- return false ;
5891
+ return util::Error{strprintf (Untranslated (" Bad snapshot data after deserializing %d coins - bad tx out value" ),
5892
+ coins_count - coins_left)};
5899
5893
}
5900
5894
coins_cache.EmplaceCoinInternalDANGER (std::move (outpoint), std::move (coin));
5901
5895
@@ -5915,7 +5909,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
5915
5909
// means <5MB of memory imprecision.
5916
5910
if (coins_processed % 120000 == 0 ) {
5917
5911
if (m_interrupt) {
5918
- return false ;
5912
+ return util::Error{ Untranslated ( " Aborting after an interrupt was requested " )} ;
5919
5913
}
5920
5914
5921
5915
const auto snapshot_cache_state = WITH_LOCK (::cs_main,
@@ -5933,9 +5927,8 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
5933
5927
}
5934
5928
}
5935
5929
} catch (const std::ios_base::failure&) {
5936
- LogPrintf (" [snapshot] bad snapshot format or truncated snapshot after deserializing %d coins\n " ,
5937
- coins_processed);
5938
- return false ;
5930
+ return util::Error{strprintf (Untranslated (" Bad snapshot format or truncated snapshot after deserializing %d coins" ),
5931
+ coins_processed)};
5939
5932
}
5940
5933
}
5941
5934
@@ -5955,9 +5948,8 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
5955
5948
out_of_coins = true ;
5956
5949
}
5957
5950
if (!out_of_coins) {
5958
- LogPrintf (" [snapshot] bad snapshot - coins left over after deserializing %d coins\n " ,
5959
- coins_count);
5960
- return false ;
5951
+ return util::Error{strprintf (Untranslated (" Bad snapshot - coins left over after deserializing %d coins" ),
5952
+ coins_count)};
5961
5953
}
5962
5954
5963
5955
LogPrintf (" [snapshot] loaded %d (%.2f MB) coins from snapshot %s\n " ,
@@ -5980,18 +5972,16 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
5980
5972
maybe_stats = ComputeUTXOStats (
5981
5973
CoinStatsHashType::HASH_SERIALIZED, snapshot_coinsdb, m_blockman, [&interrupt = m_interrupt] { SnapshotUTXOHashBreakpoint (interrupt); });
5982
5974
} catch (StopHashingException const &) {
5983
- return false ;
5975
+ return util::Error{ Untranslated ( " Aborting after an interrupt was requested " )} ;
5984
5976
}
5985
5977
if (!maybe_stats.has_value ()) {
5986
- LogPrintf (" [snapshot] failed to generate coins stats\n " );
5987
- return false ;
5978
+ return util::Error{Untranslated (" Failed to generate coins stats" )};
5988
5979
}
5989
5980
5990
5981
// Assert that the deserialized chainstate contents match the expected assumeutxo value.
5991
5982
if (AssumeutxoHash{maybe_stats->hashSerialized } != au_data.hash_serialized ) {
5992
- LogPrintf (" [snapshot] bad snapshot content hash: expected %s, got %s\n " ,
5993
- au_data.hash_serialized .ToString (), maybe_stats->hashSerialized .ToString ());
5994
- return false ;
5983
+ return util::Error{strprintf (Untranslated (" Bad snapshot content hash: expected %s, got %s" ),
5984
+ au_data.hash_serialized .ToString (), maybe_stats->hashSerialized .ToString ())};
5995
5985
}
5996
5986
5997
5987
snapshot_chainstate.m_chain .SetTip (*snapshot_start_block);
@@ -6031,7 +6021,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
6031
6021
6032
6022
LogPrintf (" [snapshot] validated snapshot (%.2f MB)\n " ,
6033
6023
coins_cache.DynamicMemoryUsage () / (1000 * 1000 ));
6034
- return true ;
6024
+ return {} ;
6035
6025
}
6036
6026
6037
6027
// Currently, this function holds cs_main for its duration, which could be for
0 commit comments