1111#include < index/coinstatsindex.h>
1212#include < serialize.h>
1313#include < uint256.h>
14- // #include <util/system.h>
1514#include < util/check.h>
15+ #include < util/overflow.h>
1616#include < util/system.h>
1717#include < validation.h>
1818
@@ -83,7 +83,9 @@ static void ApplyStats(CCoinsStats& stats, const uint256& hash, const std::map<u
8383 stats.nTransactions ++;
8484 for (auto it = outputs.begin (); it != outputs.end (); ++it) {
8585 stats.nTransactionOutputs ++;
86- stats.nTotalAmount += it->second .out .nValue ;
86+ if (stats.total_amount .has_value ()) {
87+ stats.total_amount = CheckedAdd (*stats.total_amount , it->second .out .nValue );
88+ }
8789 stats.nBogoSize += GetBogoSize (it->second .out .scriptPubKey );
8890 }
8991}
@@ -94,7 +96,9 @@ static void ApplyStats(CCoinsStats& stats, std::nullptr_t, const uint256& hash,
9496 stats.nTransactions ++;
9597 for (const auto & output : outputs) {
9698 stats.nTransactionOutputs ++;
97- stats.nTotalAmount += output.second .out .nValue ;
99+ if (stats.total_amount .has_value ()) {
100+ stats.total_amount = CheckedAdd (*stats.total_amount , output.second .out .nValue );
101+ }
98102 stats.nBogoSize += GetBogoSize (output.second .out .scriptPubKey );
99103 }
100104}
@@ -107,11 +111,9 @@ static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats&
107111 assert (pcursor);
108112
109113 if (!pindex) {
110- {
111- LOCK (cs_main);
112- assert (std::addressof (g_chainman.m_blockman ) == std::addressof (blockman));
113- pindex = blockman.LookupBlockIndex (view->GetBestBlock ());
114- }
114+ LOCK (cs_main);
115+ assert (std::addressof (g_chainman.m_blockman ) == std::addressof (blockman));
116+ pindex = blockman.LookupBlockIndex (view->GetBestBlock ());
115117 }
116118 stats.nHeight = Assert (pindex)->nHeight ;
117119 stats.hashBlock = pindex->GetBlockHash ();
0 commit comments