Skip to content

Commit 4ec0d9e

Browse files
committed
Refactor GetUTXOStats in preparation for per-COutPoint iteration
1 parent 13870b5 commit 4ec0d9e

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/rpc/blockchain.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,22 @@ struct CCoinsStats
788788
CCoinsStats() : nHeight(0), nTransactions(0), nTransactionOutputs(0), nTotalAmount(0) {}
789789
};
790790

791+
static void ApplyStats(CCoinsStats &stats, CHashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
792+
{
793+
assert(!outputs.empty());
794+
ss << hash;
795+
ss << VARINT(outputs.begin()->second.nHeight * 2 + outputs.begin()->second.fCoinBase);
796+
stats.nTransactions++;
797+
for (const auto output : outputs) {
798+
ss << VARINT(output.first + 1);
799+
ss << *(const CScriptBase*)(&output.second.out.scriptPubKey);
800+
ss << VARINT(output.second.out.nValue);
801+
stats.nTransactionOutputs++;
802+
stats.nTotalAmount += output.second.out.nValue;
803+
}
804+
ss << VARINT(0);
805+
}
806+
791807
//! Calculate statistics about the unspent transaction output set
792808
static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
793809
{
@@ -800,33 +816,25 @@ static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
800816
stats.nHeight = mapBlockIndex.find(stats.hashBlock)->second->nHeight;
801817
}
802818
ss << stats.hashBlock;
803-
CAmount nTotalAmount = 0;
804819
while (pcursor->Valid()) {
805820
boost::this_thread::interruption_point();
806821
uint256 key;
807822
CCoins coins;
808823
if (pcursor->GetKey(key) && pcursor->GetValue(coins)) {
809-
stats.nTransactions++;
810-
ss << key;
811-
ss << VARINT(coins.nHeight * 2 + coins.fCoinBase);
824+
std::map<uint32_t, Coin> outputs;
812825
for (unsigned int i=0; i<coins.vout.size(); i++) {
813-
const CTxOut &out = coins.vout[i];
826+
CTxOut &out = coins.vout[i];
814827
if (!out.IsNull()) {
815-
stats.nTransactionOutputs++;
816-
ss << VARINT(i+1);
817-
ss << *(const CScriptBase*)(&out.scriptPubKey);
818-
ss << VARINT(out.nValue);
819-
nTotalAmount += out.nValue;
828+
outputs[i] = Coin(std::move(out), coins.nHeight, coins.fCoinBase);
820829
}
821830
}
822-
ss << VARINT(0);
831+
ApplyStats(stats, ss, key, outputs);
823832
} else {
824833
return error("%s: unable to read value", __func__);
825834
}
826835
pcursor->Next();
827836
}
828837
stats.hashSerialized = ss.GetHash();
829-
stats.nTotalAmount = nTotalAmount;
830838
stats.nDiskSize = view->EstimateSize();
831839
return true;
832840
}

0 commit comments

Comments
 (0)