@@ -788,6 +788,22 @@ struct CCoinsStats
788
788
CCoinsStats () : nHeight(0 ), nTransactions(0 ), nTransactionOutputs(0 ), nTotalAmount(0 ) {}
789
789
};
790
790
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
+
791
807
// ! Calculate statistics about the unspent transaction output set
792
808
static bool GetUTXOStats (CCoinsView *view, CCoinsStats &stats)
793
809
{
@@ -800,33 +816,25 @@ static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
800
816
stats.nHeight = mapBlockIndex.find (stats.hashBlock )->second ->nHeight ;
801
817
}
802
818
ss << stats.hashBlock ;
803
- CAmount nTotalAmount = 0 ;
804
819
while (pcursor->Valid ()) {
805
820
boost::this_thread::interruption_point ();
806
821
uint256 key;
807
822
CCoins coins;
808
823
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;
812
825
for (unsigned int i=0 ; i<coins.vout .size (); i++) {
813
- const CTxOut &out = coins.vout [i];
826
+ CTxOut &out = coins.vout [i];
814
827
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 );
820
829
}
821
830
}
822
- ss << VARINT ( 0 );
831
+ ApplyStats (stats, ss, key, outputs );
823
832
} else {
824
833
return error (" %s: unable to read value" , __func__);
825
834
}
826
835
pcursor->Next ();
827
836
}
828
837
stats.hashSerialized = ss.GetHash ();
829
- stats.nTotalAmount = nTotalAmount;
830
838
stats.nDiskSize = view->EstimateSize ();
831
839
return true ;
832
840
}
0 commit comments