| 109 | //! Calculate statistics about the unspent transaction output set |
| 110 | template <typename T> |
| 111 | static std::optional<CCoinsStats> ComputeUTXOStats(T hash_obj, CCoinsView* view, node::BlockManager& blockman, const std::function<void()>& interruption_point) |
| 112 | { |
| 113 | std::unique_ptr<CCoinsViewCursor> pcursor; |
| 114 | CBlockIndex* pindex; |
| 115 | { |
| 116 | LOCK(::cs_main); |
| 117 | pcursor = view->Cursor(); |
| 118 | pindex = blockman.LookupBlockIndex(pcursor->GetBestBlock()); |
| 119 | } |
| 120 | assert(pcursor); |
| 121 | CCoinsStats stats{Assert(pindex)->nHeight, pindex->GetBlockHash()}; |
| 122 | |
| 123 | Txid prevkey; |
| 124 | std::map<uint32_t, Coin> outputs; |
| 125 | while (pcursor->Valid()) { |
| 126 | if (interruption_point) interruption_point(); |
| 127 | COutPoint key; |
| 128 | Coin coin; |
| 129 | if (pcursor->GetKey(key) && pcursor->GetValue(coin)) { |
| 130 | if (!outputs.empty() && key.hash != prevkey) { |
| 131 | ApplyStats(stats, outputs); |
| 132 | ApplyHash(hash_obj, prevkey, outputs); |
| 133 | outputs.clear(); |
| 134 | } |
| 135 | prevkey = key.hash; |
| 136 | outputs[key.n] = std::move(coin); |
| 137 | stats.coins_count++; |
| 138 | } else { |
| 139 | LogError("%s: unable to read value\n", __func__); |
| 140 | return std::nullopt; |
| 141 | } |
| 142 | pcursor->Next(); |
| 143 | } |
| 144 | if (!outputs.empty()) { |
| 145 | ApplyStats(stats, outputs); |
| 146 | ApplyHash(hash_obj, prevkey, outputs); |
| 147 | } |
| 148 | |
| 149 | FinalizeHash(hash_obj, stats); |
| 150 | |
| 151 | stats.nDiskSize = view->EstimateSize(); |
| 152 | return stats; |
| 153 | } |
| 154 | |
| 155 | std::optional<CCoinsStats> ComputeUTXOStats(CoinStatsHashType hash_type, CCoinsView* view, node::BlockManager& blockman, const std::function<void()>& interruption_point) |
| 156 | { |