@@ -862,7 +862,6 @@ static RPCHelpMan gettxoutsetinfo()
862
862
863
863
const CBlockIndex* pindex{nullptr };
864
864
const CoinStatsHashType hash_type{request.params [0 ].isNull () ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType (request.params [0 ].get_str ())};
865
- CCoinsStats stats{};
866
865
bool index_requested = request.params [2 ].isNull () || request.params [2 ].get_bool ();
867
866
868
867
NodeContext& node = EnsureAnyNodeContext (request.context );
@@ -903,7 +902,9 @@ static RPCHelpMan gettxoutsetinfo()
903
902
}
904
903
}
905
904
906
- if (GetUTXOStats (coins_view, *blockman, stats, hash_type, node.rpc_interruption_point , pindex, index_requested)) {
905
+ const std::optional<CCoinsStats> maybe_stats = GetUTXOStats (coins_view, *blockman, hash_type, node.rpc_interruption_point , pindex, index_requested);
906
+ if (maybe_stats.has_value ()) {
907
+ const CCoinsStats& stats = maybe_stats.value ();
907
908
ret.pushKV (" height" , (int64_t )stats.nHeight );
908
909
ret.pushKV (" bestblock" , stats.hashBlock .GetHex ());
909
910
ret.pushKV (" txouts" , (int64_t )stats.nTransactionOutputs );
@@ -923,9 +924,12 @@ static RPCHelpMan gettxoutsetinfo()
923
924
ret.pushKV (" total_unspendable_amount" , ValueFromAmount (stats.total_unspendable_amount ));
924
925
925
926
CCoinsStats prev_stats{};
926
-
927
927
if (pindex->nHeight > 0 ) {
928
- GetUTXOStats (coins_view, *blockman, prev_stats, hash_type, node.rpc_interruption_point , pindex->pprev , index_requested);
928
+ const std::optional<CCoinsStats> maybe_prev_stats = GetUTXOStats (coins_view, *blockman, hash_type, node.rpc_interruption_point , pindex->pprev , index_requested);
929
+ if (!maybe_prev_stats) {
930
+ throw JSONRPCError (RPC_INTERNAL_ERROR, " Unable to read UTXO set" );
931
+ }
932
+ prev_stats = maybe_prev_stats.value ();
929
933
}
930
934
931
935
UniValue block_info (UniValue::VOBJ);
@@ -2285,7 +2289,7 @@ UniValue CreateUTXOSnapshot(
2285
2289
const fs::path& temppath)
2286
2290
{
2287
2291
std::unique_ptr<CCoinsViewCursor> pcursor;
2288
- CCoinsStats stats{} ;
2292
+ std::optional< CCoinsStats> maybe_stats ;
2289
2293
const CBlockIndex* tip;
2290
2294
2291
2295
{
@@ -2305,19 +2309,20 @@ UniValue CreateUTXOSnapshot(
2305
2309
2306
2310
chainstate.ForceFlushStateToDisk ();
2307
2311
2308
- if (!GetUTXOStats (&chainstate.CoinsDB (), chainstate.m_blockman , stats, CoinStatsHashType::HASH_SERIALIZED, node.rpc_interruption_point )) {
2312
+ maybe_stats = GetUTXOStats (&chainstate.CoinsDB (), chainstate.m_blockman , CoinStatsHashType::HASH_SERIALIZED, node.rpc_interruption_point );
2313
+ if (!maybe_stats) {
2309
2314
throw JSONRPCError (RPC_INTERNAL_ERROR, " Unable to read UTXO set" );
2310
2315
}
2311
2316
2312
2317
pcursor = chainstate.CoinsDB ().Cursor ();
2313
- tip = CHECK_NONFATAL (chainstate.m_blockman .LookupBlockIndex (stats. hashBlock ));
2318
+ tip = CHECK_NONFATAL (chainstate.m_blockman .LookupBlockIndex (maybe_stats-> hashBlock ));
2314
2319
}
2315
2320
2316
2321
LOG_TIME_SECONDS (strprintf (" writing UTXO snapshot at height %s (%s) to file %s (via %s)" ,
2317
2322
tip->nHeight , tip->GetBlockHash ().ToString (),
2318
2323
fs::PathToString (path), fs::PathToString (temppath)));
2319
2324
2320
- SnapshotMetadata metadata{tip->GetBlockHash (), stats. coins_count , tip->nChainTx };
2325
+ SnapshotMetadata metadata{tip->GetBlockHash (), maybe_stats-> coins_count , tip->nChainTx };
2321
2326
2322
2327
afile << metadata;
2323
2328
@@ -2339,11 +2344,11 @@ UniValue CreateUTXOSnapshot(
2339
2344
afile.fclose ();
2340
2345
2341
2346
UniValue result (UniValue::VOBJ);
2342
- result.pushKV (" coins_written" , stats. coins_count );
2347
+ result.pushKV (" coins_written" , maybe_stats-> coins_count );
2343
2348
result.pushKV (" base_hash" , tip->GetBlockHash ().ToString ());
2344
2349
result.pushKV (" base_height" , tip->nHeight );
2345
2350
result.pushKV (" path" , path.u8string ());
2346
- result.pushKV (" txoutset_hash" , stats. hashSerialized .ToString ());
2351
+ result.pushKV (" txoutset_hash" , maybe_stats-> hashSerialized .ToString ());
2347
2352
// Cast required because univalue doesn't have serialization specified for
2348
2353
// `unsigned int`, nChainTx's type.
2349
2354
result.pushKV (" nchaintx" , uint64_t {tip->nChainTx });
0 commit comments