Skip to content

Commit

Permalink
Implemented an rpc to get the number of addresses with positive balance
Browse files Browse the repository at this point in the history
  • Loading branch information
levonpetrosyan93 committed Nov 17, 2024
1 parent a09abf0 commit 40c941c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
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());
}

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
25 changes: 25 additions & 0 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,31 @@ 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)) {
auto it = addrMap.find(key.second.hashBytes);
if (it != addrMap.end()) {
it->second += nValue;
if (it->second <= 0)
addrMap.erase(it);
} else {
if (nValue != 0)
addrMap[key.second.hashBytes] = nValue;
}
}
}
pcursor->Next();
}
return addrMap.size();
}

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();

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

0 comments on commit 40c941c

Please sign in to comment.