Return snapshot info as JSON

This commit is contained in:
Duke Leto
2018-07-17 18:42:30 +00:00
parent 920f960122
commit 92dc28a3c9
4 changed files with 38 additions and 29 deletions

View File

@@ -588,21 +588,20 @@ CBlockTreeDB *pblocktree = NULL;
#define KOMODO_ZCASH #define KOMODO_ZCASH
#include "komodo.h" #include "komodo.h"
int64_t komodo_snapshot() UniValue komodo_snapshot()
{ {
fprintf(stderr,"komodo_snapshot\n");
int64_t total = -1; int64_t total = -1;
UniValue result(UniValue::VOBJ);
if (fAddressIndex) { if (fAddressIndex) {
if ( pblocktree != 0 ) { if ( pblocktree != 0 ) {
total = pblocktree->Snapshot(); result = pblocktree->Snapshot();
} else { } else {
fprintf(stderr,"null pblocktree start with -addressindex=true\n"); fprintf(stderr,"null pblocktree start with -addressindex=true\n");
} }
} else { } else {
fprintf(stderr,"getsnapshot requires -addressindex=true\n"); fprintf(stderr,"getsnapshot requires -addressindex=true\n");
} }
fprintf(stderr,"total=%li\n", total); return(result);
return(total);
} }
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////

View File

@@ -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) UniValue getsnapshot(const UniValue& params, bool fHelp)
{ {
@@ -1026,9 +1026,13 @@ UniValue getsnapshot(const UniValue& params, bool fHelp)
"getsnapshot\n" "getsnapshot\n"
); );
} }
if ( (total= komodo_snapshot()) >= 0 ) result = komodo_snapshot();
result.push_back(Pair("total", (double)total/COIN)); if ( result.size() > 0 ) {
else result.push_back(Pair("error", "no addressindex")); // add timestamp, maybe block height?
result.push_back(Pair("time", time(NULL)));
} else {
result.push_back(Pair("error", "no addressindex"));
}
return(result); return(result);
} }

View File

@@ -10,6 +10,7 @@
#include "main.h" #include "main.h"
#include "pow.h" #include "pow.h"
#include "uint256.h" #include "uint256.h"
#include "core_io.h"
#include <stdint.h> #include <stdint.h>
@@ -398,11 +399,12 @@ bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, int type,
bool getAddressFromIndex(const int &type, const uint160 &hash, std::string &address); 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<leveldb::Iterator> pcursor(NewIterator()); boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator());
std::map <std::string, CAmount> addressAmounts; std::map <std::string, CAmount> addressAmounts;
UniValue result(UniValue::VOBJ);
//pcursor->SeekToFirst(); //pcursor->SeekToFirst();
pcursor->SeekToLast(); pcursor->SeekToLast();
@@ -425,23 +427,22 @@ int64_t CBlockTreeDB::Snapshot()
CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION); CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION);
CAmount nValue; CAmount nValue;
ssValue >> nValue; ssValue >> nValue;
//if (!getAddressFromIndex(indexKey.type, indexKey.hashBytes, address)) {
getAddressFromIndex(indexKey.type, indexKey.hashBytes, address);
std::map <std::string, CAmount>::iterator pos = addressAmounts.find(address); getAddressFromIndex(indexKey.type, indexKey.hashBytes, address);
if (pos == addressAmounts.end()) { std::map <std::string, CAmount>::iterator pos = addressAmounts.find(address);
// insert new address + utxo amount if (pos == addressAmounts.end()) {
fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue); // insert new address + utxo amount
addressAmounts[address] = nValue; fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue);
} else { addressAmounts[address] = nValue;
// update unspent tally for this address totalAddresses++;
addressAmounts[address] += nValue; } else {
} // update unspent tally for this address
addressAmounts[address] += nValue;
}
//fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN); //fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN);
total += (double) nValue / COIN; total += nValue;
//addressIndex.push_back(make_pair(indexKey, nValue)); //addressIndex.push_back(make_pair(indexKey, nValue));
//}
} catch (const std::exception& e) { } catch (const std::exception& e) {
return error("failed to get address index value"); return error("failed to get address index value");
} }
@@ -453,12 +454,16 @@ int64_t CBlockTreeDB::Snapshot()
pcursor->Prev(); pcursor->Prev();
} }
// TODO: create addresses key with array of {address,amount}
fprintf(stderr, "total=%f, totalAddresses=%li\n", (double) total / COIN, totalAddresses);
for (map <std::string, CAmount>::iterator it = addressAmounts.begin(); it != addressAmounts.end(); it++) for (map <std::string, CAmount>::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 &timestampIndex) { bool CBlockTreeDB::WriteTimestampIndex(const CTimestampIndexKey &timestampIndex) {

View File

@@ -13,6 +13,7 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <univalue.h>
class CBlockFileInfo; class CBlockFileInfo;
class CBlockIndex; class CBlockIndex;
@@ -94,7 +95,7 @@ public:
bool ReadFlag(const std::string &name, bool &fValue); bool ReadFlag(const std::string &name, bool &fValue);
bool LoadBlockIndexGuts(); bool LoadBlockIndexGuts();
bool blockOnchainActive(const uint256 &hash); bool blockOnchainActive(const uint256 &hash);
int64_t Snapshot(); UniValue Snapshot();
}; };
#endif // BITCOIN_TXDB_H #endif // BITCOIN_TXDB_H