1111#include < chainparams.h>
1212#include < coins.h>
1313#include < consensus/validation.h>
14- #include < crypto/muhash.h>
1514#include < core_io.h>
1615#include < hash.h>
1716#include < index/blockfilterindex.h>
17+ #include < index/utxosethash.h>
1818#include < policy/feerate.h>
1919#include < policy/policy.h>
2020#include < policy/rbf.h>
@@ -925,14 +925,9 @@ struct CCoinsStats
925925 CCoinsStats () : nHeight(0 ), nTransactions(0 ), nTransactionOutputs(0 ), nBogoSize(0 ), nDiskSize(0 ), nTotalAmount(0 ) {}
926926};
927927
928- static void ApplyStats (CCoinsStats &stats, MuHash3072& acc, const COutPoint outpoint, const Coin& coin)
928+ static void ApplyStats (CCoinsStats &stats, const COutPoint outpoint, const Coin& coin)
929929{
930930 stats.nTransactions ++;
931- TruncatedSHA512Writer ss (SER_DISK, 0 );
932- ss << outpoint;
933- ss << (uint32_t )(coin.nHeight * 2 + coin.fCoinBase );
934- ss << coin.out ;
935- acc *= MuHash3072 (ss.GetHash ().begin ());
936931 stats.nTransactionOutputs ++;
937932 stats.nTotalAmount += coin.out .nValue ;
938933 stats.nBogoSize += 32 /* txid */ + 4 /* vout index */ + 4 /* height + coinbase */ + 8 /* amount */ +
@@ -945,28 +940,42 @@ static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
945940 std::unique_ptr<CCoinsViewCursor> pcursor (view->Cursor ());
946941 assert (pcursor);
947942
943+ uint256 muhash_buf;
944+ const CBlockIndex* block_index;
948945 stats.hashBlock = pcursor->GetBestBlock ();
949946 {
950947 LOCK (cs_main);
951- stats. nHeight = LookupBlockIndex (stats.hashBlock )-> nHeight ;
948+ block_index = LookupBlockIndex (stats.hashBlock );
952949 }
953- MuHash3072 acc;
950+
951+ stats.nHeight = block_index->nHeight ;
952+
953+ bool index_ready = g_utxo_set_hash->BlockUntilSyncedToCurrentChain ();
954+
955+ if (!g_utxo_set_hash->LookupHash (block_index, muhash_buf)) {
956+ int err_code = RPC_MISC_ERROR;
957+
958+ if (!index_ready) {
959+ throw JSONRPCError (err_code, " UTXO set hash is still in the process of being indexed." );
960+ } else {
961+ throw JSONRPCError (err_code, " Unable to get UTXO set hash" );
962+ }
963+ }
964+
965+ stats.muhash = muhash_buf;
966+
954967 while (pcursor->Valid ()) {
955968 boost::this_thread::interruption_point ();
956969 COutPoint key;
957970 Coin coin;
958971 if (pcursor->GetKey (key) && pcursor->GetValue (coin)) {
959- ApplyStats (stats, acc, key, coin);
972+ ApplyStats (stats, key, coin);
960973 } else {
961974 return error (" %s: unable to read value" , __func__);
962975 }
963976 pcursor->Next ();
964977 }
965- unsigned char data[384 ];
966- acc.Finalize (data);
967- TruncatedSHA512Writer ss (SER_DISK, 0 );
968- ss << FLATDATA (data);
969- stats.muhash = ss.GetHash ();
978+
970979 stats.nDiskSize = view->EstimateSize ();
971980 return true ;
972981}
0 commit comments