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
#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);
}
//////////////////////////////////////////////////////////////////////////////

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)
{
@@ -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);
}

View File

@@ -10,6 +10,7 @@
#include "main.h"
#include "pow.h"
#include "uint256.h"
#include "core_io.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);
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());
std::map <std::string, CAmount> 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 <std::string, CAmount>::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 <std::string, CAmount>::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 <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) {

View File

@@ -13,6 +13,7 @@
#include <string>
#include <utility>
#include <vector>
#include <univalue.h>
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