From a4d41984f67bcc7472469b564de92b8ab1505eca Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Wed, 18 Jul 2018 19:44:16 +0000 Subject: [PATCH] Modify how we iterate and deal with the end of iteration, since iter->Valid seems to always be true --- src/txdb.cpp | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/txdb.cpp b/src/txdb.cpp index 5608cbb07..af6cd0fbf 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -402,61 +402,59 @@ 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; - boost::scoped_ptr pcursor(NewIterator()); + boost::scoped_ptr iter(NewIterator()); std::map addressAmounts; UniValue result(UniValue::VOBJ); result.push_back(Pair("start_time", time(NULL))); - //pcursor->SeekToFirst(); - pcursor->SeekToLast(); - int64_t startingHeight = chainActive.Height(); - //fprintf(stderr, "starting_height=%li\n", startingHeight); - - while (pcursor->Valid()) + fprintf(stderr, "Starting snapshot at height %li\n", startingHeight); + for (iter->SeekToLast(); iter->Valid(); iter->Prev()) { boost::this_thread::interruption_point(); try { - leveldb::Slice slKey = pcursor->key(); + leveldb::Slice slKey = iter->key(); CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION); CAddressIndexIteratorKey indexKey; - ssKey >> chType; - ssKey >> indexKey; + + ssKey >> chType; + ssKey >> indexKey; //fprintf(stderr, "chType=%d\n", chType); if (chType == DB_ADDRESSUNSPENTINDEX) { try { - leveldb::Slice slValue = pcursor->value(); + leveldb::Slice slValue = iter->value(); CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION); CAmount nValue; ssValue >> 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); + //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 + //fprintf(stderr, "updating address %s with new utxo amount %li\n", address.c_str(), nValue); addressAmounts[address] += nValue; } - //fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN); total += nValue; + } catch (const std::exception& e) { - // this should not happen normally, but maybe if an index is corrupt - fprintf(stderr, "failed to get address index value\n"); + fprintf(stderr, "DONE %s: LevelDB addressindex exception! - %s\n", __func__, e.what()); + break; } - } + } } catch (const std::exception& e) { - fprintf(stderr, "%s: LevelDB exception! - %s\n", __func__, e.what()); + fprintf(stderr, "DONE reading index entries\n"); break; } - pcursor->Prev(); } UniValue addresses(UniValue::VARR);