Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented an rpc to get the number of addresses with positive balance #1496

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "getspentinfo", 0},
{ "getaddresstxids", 0},
{ "getaddressbalance", 0},
{ "getAddressNumWBalance", 0},
{ "getaddressdeltas", 0},
{ "getaddressutxos", 0},
{ "getaddressmempool", 0},
Expand Down
14 changes: 14 additions & 0 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,20 @@ UniValue getaddressbalance(const JSONRPCRequest& request)

}

UniValue getAddressNumWBalance(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() > 0)
throw std::runtime_error(
"getAddressNumWBalance\n"
"Gives the number of addresses which has positive balance."
);
if (!GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX))
throw std::runtime_error(
"You have to reindex with -addressindex flag to get an accurate result.");

return uint64_t(pblocktree->findAddressNumWBalance());
}
levonpetrosyan93 marked this conversation as resolved.
Show resolved Hide resolved

UniValue getanonymityset(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 2)
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ static const CRPCCommand vRPCCommands[] =
{ "addressindex", "getaddressdeltas", &getaddressdeltas, false },
{ "addressindex", "getaddresstxids", &getaddresstxids, false },
{ "addressindex", "getaddressbalance", &getaddressbalance, false },
{ "addressindex", "getAddressNumWBalance", &getAddressNumWBalance, false },

/* Mobile related */
{ "mobile", "getanonymityset", &getanonymityset, false },
{ "mobile", "getmintmetadata", &getmintmetadata, true },
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ extern UniValue getaddressutxos(const JSONRPCRequest &request);
extern UniValue getaddressdeltas(const JSONRPCRequest &request);
extern UniValue getaddresstxids(const JSONRPCRequest &request);
extern UniValue getaddressbalance(const JSONRPCRequest &request);
extern UniValue getAddressNumWBalance(const JSONRPCRequest &request);


extern UniValue getanonymityset(const JSONRPCRequest& params);
extern UniValue getmintmetadata(const JSONRPCRequest& params);
Expand Down
29 changes: 29 additions & 0 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,35 @@ bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, AddressType type,
return true;
}

size_t CBlockTreeDB::findAddressNumWBalance() {
boost::scoped_ptr<CDBIterator> pcursor(NewIterator());
pcursor->SeekToFirst();
std::unordered_map<uint160, CAmount> addrMap;
while (pcursor->Valid()) {
boost::this_thread::interruption_point();
std::pair<char,CAddressIndexKey> key;
if (pcursor->GetKey(key) && key.first == DB_ADDRESSINDEX && (key.second.type == AddressType::payToPubKeyHash || key.second.type == AddressType::payToExchangeAddress)) {
CAmount nValue;
if (pcursor->GetValue(nValue)) {
CAmount nValue;
// Retrieve the associated value
if (pcursor->GetValue(nValue) && nValue != 0) { // Only process non-zero values
addrMap[key.second.hashBytes] += nValue; // Accumulate balance for the address
}
}
levonpetrosyan93 marked this conversation as resolved.
Show resolved Hide resolved
}
pcursor->Next();
}

size_t counter = 0;
for (auto& itr : addrMap) {
if (itr.second > 0) {
++counter;
}
}

return counter;
}
levonpetrosyan93 marked this conversation as resolved.
Show resolved Hide resolved

bool CBlockTreeDB::WriteTimestampIndex(const CTimestampIndexKey &timestampIndex) {
CDBBatch batch(*this);
Expand Down
1 change: 1 addition & 0 deletions src/txdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class CBlockTreeDB : public CDBWrapper
bool ReadAddressIndex(uint160 addressHash, AddressType type,
std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex,
int start = 0, int end = 0);
size_t findAddressNumWBalance();
levonpetrosyan93 marked this conversation as resolved.
Show resolved Hide resolved

bool WriteTimestampIndex(const CTimestampIndexKey &timestampIndex);
bool ReadTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector<uint256> &vect);
Expand Down
Loading