From 20ba439d0541ed7b74ff4c647a08286f1c4b30ac Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Thu, 19 Jul 2018 05:37:27 +0000 Subject: [PATCH 1/2] Add number of utxos to metadata in json response --- src/txdb.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/txdb.cpp b/src/txdb.cpp index af6cd0fbf..8ae2caa7b 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -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 iter(NewIterator()); std::map 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, std::pair)> Comparator; Comparator compFunctor = [](std::pair elem1 ,std::pair elem2) { @@ -467,6 +468,8 @@ extern UniValue CBlockTreeDB::Snapshot() // This is our intermediate data structure that allows us to calculate addressSorted std::set, 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 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 From c4a73444de805e55c0bd083dc17cb4441722c386 Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Fri, 20 Jul 2018 03:58:05 +0000 Subject: [PATCH 2/2] Fix bug where some addresses are lost in sorting by using a vector instead of set and avoiding custom comparison function --- src/txdb.cpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/txdb.cpp b/src/txdb.cpp index 8ae2caa7b..ab3397049 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -405,6 +405,7 @@ extern UniValue CBlockTreeDB::Snapshot() int64_t utxos = 0; boost::scoped_ptr iter(NewIterator()); std::map addressAmounts; + std::vector > vaddr; UniValue result(UniValue::VOBJ); result.push_back(Pair("start_time", time(NULL))); @@ -461,22 +462,18 @@ extern UniValue CBlockTreeDB::Snapshot() UniValue addresses(UniValue::VARR); fprintf(stderr, "total=%f, totalAddresses=%li, utxos=%li\n", (double) total / COIN, totalAddresses, utxos); - typedef std::function, std::pair)> Comparator; - Comparator compFunctor = [](std::pair elem1 ,std::pair elem2) { - return elem1.second > elem2.second; /* descending */ - }; - // This is our intermediate data structure that allows us to calculate addressSorted - std::set, Comparator> sortedSnapshot(addressAmounts.begin(), addressAmounts.end(), compFunctor); - - //fprintf(stderr, "sortedSnapshot.size=%li\n", sortedSnapshot.size() ); + for (std::pair element : addressAmounts) { + vaddr.push_back( make_pair(element.second, element.first) ); + } + std::sort(vaddr.begin(), vaddr.end()); UniValue obj(UniValue::VOBJ); UniValue addressesSorted(UniValue::VARR); - for (std::pair element : sortedSnapshot) { + for (std::vector>::iterator it = vaddr.begin(); it!=vaddr.end(); ++it) { UniValue obj(UniValue::VOBJ); - obj.push_back( make_pair("addr", element.first.c_str() ) ); + obj.push_back( make_pair("addr", it->second.c_str() ) ); char amount[32]; - sprintf(amount, "%.8f", (double) element.second / COIN); + sprintf(amount, "%.8f", (double) it->first / COIN); obj.push_back( make_pair("amount", amount) ); addressesSorted.push_back(obj); }