Skip to content

Commit

Permalink
Change count type used by gettxoutsetinfo
Browse files Browse the repository at this point in the history
Fixed an issue where negative numbers were displayed when obtaining Circulating supply
changing CAmount (is a int64_t) to arith_uint256 for nTotalAmount in CCoinsStats to prevent overflow
Because the maximum value when using int64_t is 9223372036854775807
The code is referenced from Dogecoin
  • Loading branch information
infinitecoin-project committed Mar 7, 2024
1 parent e310aa6 commit 11f3cc5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ struct CCoinsStats
uint64_t nTransactionOutputs;
uint64_t nSerializedSize;
uint256 hashSerialized;
CAmount nTotalAmount;
arith_uint256 nTotalAmount;

CCoinsStats() : nHeight(0), nTransactions(0), nTransactionOutputs(0), nSerializedSize(0), nTotalAmount(0) {}
};
Expand All @@ -791,7 +791,7 @@ static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
stats.nHeight = mapBlockIndex.find(stats.hashBlock)->second->nHeight;
}
ss << stats.hashBlock;
CAmount nTotalAmount = 0;
arith_uint256 nTotalAmount = 0;
while (pcursor->Valid()) {
boost::this_thread::interruption_point();
uint256 key;
Expand Down
10 changes: 10 additions & 0 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ UniValue ValueFromAmount(const CAmount& amount)
strprintf("%s%d.%08d", sign ? "-" : "", quotient, remainder));
}

UniValue ValueFromAmount(const arith_uint256& amount)
{
bool sign = amount < 0;
arith_uint256 n_abs = (sign ? -amount : amount);
arith_uint256 quotient = n_abs / COIN;
arith_uint256 remainder = n_abs - (quotient * COIN);
return UniValue(UniValue::VNUM,
strprintf("%s%d.%08d", sign ? "-" : "", (int64_t)quotient.getdouble(), (int64_t)remainder.getdouble()));
}

uint256 ParseHashV(const UniValue& v, string strName)
{
string strHex;
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define BITCOIN_RPCSERVER_H

#include "amount.h"
#include "arith_uint256.h"
#include "rpc/protocol.h"
#include "uint256.h"

Expand Down Expand Up @@ -193,6 +194,7 @@ extern std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKe
extern int64_t nWalletUnlockTime;
extern CAmount AmountFromValue(const UniValue& value);
extern UniValue ValueFromAmount(const CAmount& amount);
extern UniValue ValueFromAmount(const arith_uint256& amount);
extern double GetDifficulty(const CBlockIndex* blockindex = NULL);
extern std::string HelpRequiringPassphrase();
extern std::string HelpExampleCli(const std::string& methodname, const std::string& args);
Expand Down

1 comment on commit 11f3cc5

@infinitecoin-project
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

04:12:28

gettxoutsetinfo

04:13:12

{
"height": 9983425,
"bestblock": "7b20731882fb65aebf844e3db9e3885b290283e0cb428f563cd66247bf2d5c80",
"transactions": 8423179,
"txouts": 22773931,
"bytes_serialized": 911853004,
"hash_serialized": "e82d5a785ba8a48c1e0b46db8f64aba0a6f0375af64b81004cd3d21249bb20ba",
"total_amount": 94568273021.14715231
}

Please sign in to comment.