Add number of utxos to metadata in json response

This commit is contained in:
Duke Leto
2018-07-19 05:37:27 +00:00
parent a4d41984f6
commit 20ba439d05

View File

@@ -402,6 +402,7 @@ bool getAddressFromIndex(const int &type, const uint160 &hash, std::string &addr
extern UniValue CBlockTreeDB::Snapshot()
{
char chType; int64_t total = 0; int64_t totalAddresses = 0; std::string address;
int64_t utxos = 0;
boost::scoped_ptr<leveldb::Iterator> iter(NewIterator());
std::map <std::string, CAmount> addressAmounts;
UniValue result(UniValue::VOBJ);
@@ -445,7 +446,7 @@ extern UniValue CBlockTreeDB::Snapshot()
}
//fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN);
total += nValue;
utxos++;
} catch (const std::exception& e) {
fprintf(stderr, "DONE %s: LevelDB addressindex exception! - %s\n", __func__, e.what());
break;
@@ -458,7 +459,7 @@ extern UniValue CBlockTreeDB::Snapshot()
}
UniValue addresses(UniValue::VARR);
fprintf(stderr, "total=%f, totalAddresses=%li\n", (double) total / COIN, totalAddresses);
fprintf(stderr, "total=%f, totalAddresses=%li, utxos=%li\n", (double) total / COIN, totalAddresses, utxos);
typedef std::function<bool(std::pair<std::string, CAmount>, std::pair<std::string, CAmount>)> Comparator;
Comparator compFunctor = [](std::pair<std::string, CAmount> elem1 ,std::pair<std::string, CAmount> elem2) {
@@ -467,6 +468,8 @@ extern UniValue CBlockTreeDB::Snapshot()
// This is our intermediate data structure that allows us to calculate addressSorted
std::set<std::pair<std::string, CAmount>, Comparator> sortedSnapshot(addressAmounts.begin(), addressAmounts.end(), compFunctor);
//fprintf(stderr, "sortedSnapshot.size=%li\n", sortedSnapshot.size() );
UniValue obj(UniValue::VOBJ);
UniValue addressesSorted(UniValue::VARR);
for (std::pair<std::string, CAmount> element : sortedSnapshot) {
@@ -480,9 +483,11 @@ extern UniValue CBlockTreeDB::Snapshot()
if (totalAddresses > 0) {
result.push_back(make_pair("addresses", addressesSorted));
result.push_back(make_pair("total", total / COIN ));
result.push_back(make_pair("total", (double) total / COIN ));
result.push_back(make_pair("average",(double) (total/COIN) / totalAddresses ));
}
// Total number of utxos in this snaphot
result.push_back(make_pair("utxos", utxos));
// Total number of addresses in this snaphot
result.push_back(make_pair("total_addresses", totalAddresses));
// The snapshot began at this block height