Refactor GetUTXOStats in preparation for per-COutPoint iteration

This commit is contained in:
Pieter Wuille 2017-04-26 16:39:58 -07:00
parent 13870b56fc
commit 4ec0d9e794

View File

@ -788,6 +788,22 @@ struct CCoinsStats
CCoinsStats() : nHeight(0), nTransactions(0), nTransactionOutputs(0), nTotalAmount(0) {} CCoinsStats() : nHeight(0), nTransactions(0), nTransactionOutputs(0), nTotalAmount(0) {}
}; };
static void ApplyStats(CCoinsStats &stats, CHashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
{
assert(!outputs.empty());
ss << hash;
ss << VARINT(outputs.begin()->second.nHeight * 2 + outputs.begin()->second.fCoinBase);
stats.nTransactions++;
for (const auto output : outputs) {
ss << VARINT(output.first + 1);
ss << *(const CScriptBase*)(&output.second.out.scriptPubKey);
ss << VARINT(output.second.out.nValue);
stats.nTransactionOutputs++;
stats.nTotalAmount += output.second.out.nValue;
}
ss << VARINT(0);
}
//! Calculate statistics about the unspent transaction output set //! Calculate statistics about the unspent transaction output set
static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats) static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
{ {
@ -800,33 +816,25 @@ static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
stats.nHeight = mapBlockIndex.find(stats.hashBlock)->second->nHeight; stats.nHeight = mapBlockIndex.find(stats.hashBlock)->second->nHeight;
} }
ss << stats.hashBlock; ss << stats.hashBlock;
CAmount nTotalAmount = 0;
while (pcursor->Valid()) { while (pcursor->Valid()) {
boost::this_thread::interruption_point(); boost::this_thread::interruption_point();
uint256 key; uint256 key;
CCoins coins; CCoins coins;
if (pcursor->GetKey(key) && pcursor->GetValue(coins)) { if (pcursor->GetKey(key) && pcursor->GetValue(coins)) {
stats.nTransactions++; std::map<uint32_t, Coin> outputs;
ss << key;
ss << VARINT(coins.nHeight * 2 + coins.fCoinBase);
for (unsigned int i=0; i<coins.vout.size(); i++) { for (unsigned int i=0; i<coins.vout.size(); i++) {
const CTxOut &out = coins.vout[i]; CTxOut &out = coins.vout[i];
if (!out.IsNull()) { if (!out.IsNull()) {
stats.nTransactionOutputs++; outputs[i] = Coin(std::move(out), coins.nHeight, coins.fCoinBase);
ss << VARINT(i+1);
ss << *(const CScriptBase*)(&out.scriptPubKey);
ss << VARINT(out.nValue);
nTotalAmount += out.nValue;
} }
} }
ss << VARINT(0); ApplyStats(stats, ss, key, outputs);
} else { } else {
return error("%s: unable to read value", __func__); return error("%s: unable to read value", __func__);
} }
pcursor->Next(); pcursor->Next();
} }
stats.hashSerialized = ss.GetHash(); stats.hashSerialized = ss.GetHash();
stats.nTotalAmount = nTotalAmount;
stats.nDiskSize = view->EstimateSize(); stats.nDiskSize = view->EstimateSize();
return true; return true;
} }