Modify how we iterate and deal with the end of iteration, since iter->Valid seems to always be true

This commit is contained in:
Duke Leto
2018-07-18 19:44:16 +00:00
parent 3350160a6c
commit a4d41984f6

View File

@@ -402,61 +402,59 @@ bool getAddressFromIndex(const int &type, const uint160 &hash, std::string &addr
extern UniValue CBlockTreeDB::Snapshot() extern UniValue CBlockTreeDB::Snapshot()
{ {
char chType; int64_t total = 0; int64_t totalAddresses = 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> iter(NewIterator());
std::map <std::string, CAmount> addressAmounts; std::map <std::string, CAmount> addressAmounts;
UniValue result(UniValue::VOBJ); UniValue result(UniValue::VOBJ);
result.push_back(Pair("start_time", time(NULL))); result.push_back(Pair("start_time", time(NULL)));
//pcursor->SeekToFirst();
pcursor->SeekToLast();
int64_t startingHeight = chainActive.Height(); int64_t startingHeight = chainActive.Height();
//fprintf(stderr, "starting_height=%li\n", startingHeight); fprintf(stderr, "Starting snapshot at height %li\n", startingHeight);
for (iter->SeekToLast(); iter->Valid(); iter->Prev())
while (pcursor->Valid())
{ {
boost::this_thread::interruption_point(); boost::this_thread::interruption_point();
try try
{ {
leveldb::Slice slKey = pcursor->key(); leveldb::Slice slKey = iter->key();
CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION); CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION);
CAddressIndexIteratorKey indexKey; CAddressIndexIteratorKey indexKey;
ssKey >> chType;
ssKey >> indexKey; ssKey >> chType;
ssKey >> indexKey;
//fprintf(stderr, "chType=%d\n", chType); //fprintf(stderr, "chType=%d\n", chType);
if (chType == DB_ADDRESSUNSPENTINDEX) if (chType == DB_ADDRESSUNSPENTINDEX)
{ {
try { try {
leveldb::Slice slValue = pcursor->value(); leveldb::Slice slValue = iter->value();
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;
getAddressFromIndex(indexKey.type, indexKey.hashBytes, address); getAddressFromIndex(indexKey.type, indexKey.hashBytes, address);
std::map <std::string, CAmount>::iterator pos = addressAmounts.find(address); std::map <std::string, CAmount>::iterator pos = addressAmounts.find(address);
if (pos == addressAmounts.end()) { if (pos == addressAmounts.end()) {
// insert new address + utxo amount // insert new address + utxo amount
fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue); //fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue);
addressAmounts[address] = nValue; addressAmounts[address] = nValue;
totalAddresses++; totalAddresses++;
} else { } else {
// update unspent tally for this address // update unspent tally for this address
//fprintf(stderr, "updating address %s with new utxo amount %li\n", address.c_str(), nValue);
addressAmounts[address] += nValue; 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 += nValue; total += nValue;
} catch (const std::exception& e) { } catch (const std::exception& e) {
// this should not happen normally, but maybe if an index is corrupt fprintf(stderr, "DONE %s: LevelDB addressindex exception! - %s\n", __func__, e.what());
fprintf(stderr, "failed to get address index value\n"); break;
} }
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
fprintf(stderr, "%s: LevelDB exception! - %s\n", __func__, e.what()); fprintf(stderr, "DONE reading index entries\n");
break; break;
} }
pcursor->Prev();
} }
UniValue addresses(UniValue::VARR); UniValue addresses(UniValue::VARR);