diff --git a/src/main.cpp b/src/main.cpp index 15d74adb2..dfebfec4f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -588,21 +588,20 @@ CBlockTreeDB *pblocktree = NULL; #define KOMODO_ZCASH #include "komodo.h" -int64_t komodo_snapshot() +UniValue komodo_snapshot() { - fprintf(stderr,"komodo_snapshot\n"); int64_t total = -1; + UniValue result(UniValue::VOBJ); if (fAddressIndex) { if ( pblocktree != 0 ) { - total = pblocktree->Snapshot(); + result = pblocktree->Snapshot(); } else { fprintf(stderr,"null pblocktree start with -addressindex=true\n"); } } else { fprintf(stderr,"getsnapshot requires -addressindex=true\n"); } - fprintf(stderr,"total=%li\n", total); - return(total); + return(result); } ////////////////////////////////////////////////////////////////////////////// diff --git a/src/rpcmisc.cpp b/src/rpcmisc.cpp index bf7f345d5..198b41090 100644 --- a/src/rpcmisc.cpp +++ b/src/rpcmisc.cpp @@ -1015,7 +1015,7 @@ UniValue getaddressbalance(const UniValue& params, bool fHelp) } -int32_t komodo_snapshot(); +UniValue komodo_snapshot(); UniValue getsnapshot(const UniValue& params, bool fHelp) { @@ -1026,9 +1026,13 @@ UniValue getsnapshot(const UniValue& params, bool fHelp) "getsnapshot\n" ); } - if ( (total= komodo_snapshot()) >= 0 ) - result.push_back(Pair("total", (double)total/COIN)); - else result.push_back(Pair("error", "no addressindex")); + result = komodo_snapshot(); + if ( result.size() > 0 ) { + // add timestamp, maybe block height? + result.push_back(Pair("time", time(NULL))); + } else { + result.push_back(Pair("error", "no addressindex")); + } return(result); } diff --git a/src/txdb.cpp b/src/txdb.cpp index 00d3d276b..63b640123 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -10,6 +10,7 @@ #include "main.h" #include "pow.h" #include "uint256.h" +#include "core_io.h" #include @@ -398,11 +399,12 @@ bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, int type, bool getAddressFromIndex(const int &type, const uint160 &hash, std::string &address); -int64_t CBlockTreeDB::Snapshot() +extern UniValue CBlockTreeDB::Snapshot() { - char chType; int64_t total = 0; std::string address; + char chType; int64_t total = 0; int64_t totalAddresses = 0; std::string address; boost::scoped_ptr pcursor(NewIterator()); std::map addressAmounts; + UniValue result(UniValue::VOBJ); //pcursor->SeekToFirst(); pcursor->SeekToLast(); @@ -425,23 +427,22 @@ int64_t CBlockTreeDB::Snapshot() CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION); CAmount nValue; ssValue >> nValue; - //if (!getAddressFromIndex(indexKey.type, indexKey.hashBytes, address)) { - getAddressFromIndex(indexKey.type, indexKey.hashBytes, address); - std::map ::iterator pos = addressAmounts.find(address); - if (pos == addressAmounts.end()) { - // insert new address + utxo amount - fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue); - addressAmounts[address] = nValue; - } else { - // update unspent tally for this address - addressAmounts[address] += nValue; - } + getAddressFromIndex(indexKey.type, indexKey.hashBytes, address); + std::map ::iterator pos = addressAmounts.find(address); + if (pos == addressAmounts.end()) { + // insert new address + utxo amount + fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue); + addressAmounts[address] = nValue; + totalAddresses++; + } else { + // update unspent tally for this address + addressAmounts[address] += nValue; + } - //fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN); - total += (double) nValue / COIN; - //addressIndex.push_back(make_pair(indexKey, nValue)); - //} + //fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN); + total += nValue; + //addressIndex.push_back(make_pair(indexKey, nValue)); } catch (const std::exception& e) { return error("failed to get address index value"); } @@ -453,12 +454,16 @@ int64_t CBlockTreeDB::Snapshot() pcursor->Prev(); } + // TODO: create addresses key with array of {address,amount} + + fprintf(stderr, "total=%f, totalAddresses=%li\n", (double) total / COIN, totalAddresses); for (map ::iterator it = addressAmounts.begin(); it != addressAmounts.end(); it++) { - fprintf(stderr,"{\"%s\", %.8f},\n",it->first.c_str(),(double) it->second / COIN); + fprintf(stderr,"{\"%s\", %.8f},\n",it->first.c_str(),(double) it->second / COIN); + result.push_back(make_pair( it->first.c_str(), (double) it->second / COIN ) ); } - return(total); + return(result); } bool CBlockTreeDB::WriteTimestampIndex(const CTimestampIndexKey ×tampIndex) { diff --git a/src/txdb.h b/src/txdb.h index cb4d6d19e..6c6b41917 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -13,6 +13,7 @@ #include #include #include +#include class CBlockFileInfo; class CBlockIndex; @@ -94,7 +95,7 @@ public: bool ReadFlag(const std::string &name, bool &fValue); bool LoadBlockIndexGuts(); bool blockOnchainActive(const uint256 &hash); - int64_t Snapshot(); + UniValue Snapshot(); }; #endif // BITCOIN_TXDB_H